tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/gps/gpsparser_test.go (about)

     1  package gps
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	qt "github.com/frankban/quicktest"
     8  )
     9  
    10  func TestParseUnknownSentence(t *testing.T) {
    11  	c := qt.New(t)
    12  
    13  	p := NewParser()
    14  
    15  	val := "$GPGSV,3,1,09,07,14,317,22,08,31,284,25,10,32,133,39,16,85,232,29*7F"
    16  	_, err := p.Parse(val)
    17  	c.Assert(err.Error(), qt.Contains, "unsupported NMEA sentence type")
    18  }
    19  
    20  func TestParseGGA(t *testing.T) {
    21  	c := qt.New(t)
    22  
    23  	p := NewParser()
    24  
    25  	val := "$GPGGA,115739.00,4158.8441367,N,09147.4416929,"
    26  	fix, err := p.Parse(val)
    27  	if err != errInvalidGGASentence {
    28  		t.Error("should have errInvalidGGASentence error")
    29  	}
    30  
    31  	val = "$GPGGA,115739.00,4158.8441367,N,09147.4416929,W,4,13,0.9,255.747,M,-32.00,M,01,0000*6E"
    32  	fix, err = p.Parse(val)
    33  	if err != nil {
    34  		t.Error("should have parsed")
    35  	}
    36  	c.Assert(fix.Latitude, qt.Equals, float32(41.980735778808594))
    37  	c.Assert(fix.Longitude, qt.Equals, float32(-91.79069519042969))
    38  	c.Assert(fix.Altitude, qt.Equals, int32(255))
    39  }
    40  
    41  func TestParseGLL(t *testing.T) {
    42  	c := qt.New(t)
    43  
    44  	p := NewParser()
    45  
    46  	val := "$GPGLL,3953.88008971,N,10506.7531891"
    47  	_, err := p.Parse(val)
    48  	if err != errInvalidGLLSentence {
    49  		t.Error("should have errInvalidGLLSentence error")
    50  	}
    51  
    52  	val = "$GPGLL,5109.0262317,N,11401.8407304,W,202725.00,A,D*79"
    53  	fix, err := p.Parse(val)
    54  	if err != nil {
    55  		t.Error("should have parsed")
    56  	}
    57  
    58  	c.Assert(fix.Latitude, qt.Equals, float32(51.15043640136719))
    59  	c.Assert(fix.Longitude, qt.Equals, float32(-114.03067779541016))
    60  }
    61  
    62  func TestParseRMC(t *testing.T) {
    63  	c := qt.New(t)
    64  
    65  	p := NewParser()
    66  
    67  	val := "$GPRMC,203522.00,A,5109.0262308,N,11401.8407342,"
    68  	_, err := p.Parse(val)
    69  	if err != errInvalidRMCSentence {
    70  		t.Error("should have errInvalidRMCSentence error")
    71  	}
    72  
    73  	val = "$GPRMC,203522.00,A,5109.0262308,N,11401.8407342,W,0.004,133.4,130522,0.0,E,D*2B"
    74  	fix, err := p.Parse(val)
    75  	if err != nil {
    76  		t.Error("should have parsed")
    77  	}
    78  
    79  	c.Assert(fix.Time.Year(), qt.Equals, 2022)
    80  	c.Assert(fix.Time.Month(), qt.Equals, time.May)
    81  	c.Assert(fix.Time.Day(), qt.Equals, 13)
    82  	c.Assert(fix.Time.Hour(), qt.Equals, 20)
    83  	c.Assert(fix.Time.Minute(), qt.Equals, 35)
    84  	c.Assert(fix.Time.Second(), qt.Equals, 22)
    85  	c.Assert(fix.Latitude, qt.Equals, float32(51.15043640136719))
    86  	c.Assert(fix.Longitude, qt.Equals, float32(-114.03067779541016))
    87  }
    88  
    89  func TestTime(t *testing.T) {
    90  	c := qt.New(t)
    91  
    92  	val := ""
    93  	tm := findTime(val)
    94  	c.Assert(tm, qt.Equals, time.Time{})
    95  
    96  	val = "225446"
    97  	tm = findTime(val)
    98  	c.Assert(tm, qt.Equals, time.Date(0, 0, 0, 22, 54, 46, 0, time.UTC))
    99  
   100  	val = "124326.02752"
   101  	tm = findTime(val)
   102  	c.Assert(tm, qt.Equals, time.Date(0, 0, 0, 12, 43, 26, 2752, time.UTC))
   103  }