-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,72 @@ | ||
package nmea | ||
|
||
import ( | ||
"testing" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestChecksumOK(t *testing.T) { | ||
s := Sentence{Raw: "$GPFOO,1,2,3.3,x,y,zz,*51", Checksum: "51"} | ||
assert.NoError(t, s.sumOk(), "Checksum check failed") | ||
s := Sentence{Raw: "$GPFOO,1,2,3.3,x,y,zz,*51", Checksum: "51"} | ||
assert.NoError(t, s.sumOk(), "Checksum check failed") | ||
} | ||
|
||
func TestChecksumBad(t *testing.T) { | ||
s := Sentence{Raw: "$GPFOO,1,2,3.3,x,y,zz,*51", Checksum: "2C"} | ||
expected := "[51 != 2C]" | ||
assert.Error(t, s.sumOk(), "Expected '%s'", expected) | ||
s := Sentence{Raw: "$GPFOO,1,2,3.3,x,y,zz,*51", Checksum: "2C"} | ||
assert.Error(t, s.sumOk(), "Expected '[51 != 2C]'") | ||
} | ||
|
||
func TestChecksumBadRaw(t *testing.T) { | ||
badRaw := "$GPFOO,1,2,3.3,x,y,zz,*33" | ||
expected := "Sentence checksum mismatch [51 != 33]" | ||
_, err := Parse(badRaw) | ||
assert.Error(t, err, "Expected '%s'", expected) | ||
badRaw := "$GPFOO,1,2,3.3,x,y,zz,*33" | ||
_, err := Parse(badRaw) | ||
assert.Error(t, err, "Expected 'Sentence checksum mismatch [51 != 33]'") | ||
} | ||
|
||
func TestBadStartCharacter(t *testing.T) { | ||
// Check that a bad start character is flagged. | ||
rawBadStart := "%GPFOO,1,2,3,x,y,z*1A" | ||
expected := "Sentence does not contain a '$'" | ||
_, err := Parse(rawBadStart) | ||
assert.Error(t, err, "Expected '%s'", expected) | ||
// Check that a bad start character is flagged. | ||
rawBadStart := "%GPFOO,1,2,3,x,y,z*1A" | ||
_, err := Parse(rawBadStart) | ||
assert.Error(t, err, "Expected 'Sentence does not contain a '$''") | ||
} | ||
|
||
func TestBadChecksumDelimiter(t *testing.T) { | ||
// Check that a bad checksum delimiter is flagged. | ||
rawBadSumSep := "$GPFOO,1,2,3,x,y,z" | ||
expected := "Sentence does not contain single checksum separator" | ||
_, err := Parse(rawBadSumSep) | ||
assert.Error(t, err, "Expected '%s'", expected) | ||
// Check that a bad checksum delimiter is flagged. | ||
rawBadSumSep := "$GPFOO,1,2,3,x,y,z" | ||
_, err := Parse(rawBadSumSep) | ||
assert.Error(t, err, "Expected 'Sentence does not contain single checksum separator'") | ||
} | ||
|
||
func TestGoodParsing(t *testing.T) { | ||
// Check for good parsing. | ||
raw := "$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0C" | ||
_, err := Parse(raw) | ||
assert.NoError(t, err, "Parse error") | ||
// Check for good parsing. | ||
raw := "$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0C" | ||
_, err := Parse(raw) | ||
assert.NoError(t, err, "Parse error") | ||
} | ||
|
||
func TestGoodFields(t *testing.T) { | ||
raw := "$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0C" | ||
expectedFields := []string{"235236", "A", "3925.9479", "N", "11945.9211", "W", "44.7", "153.6", "250905", "15.2", "E", "A"} | ||
m, _ := Parse(raw) | ||
assert.EqualValues(t, expectedFields, m.GetSentence().Fields, "Got '%q', expected '%q'", m.GetSentence().Fields, expectedFields) | ||
raw := "$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0C" | ||
expectedFields := []string{"235236", "A", "3925.9479", "N", "11945.9211", "W", "44.7", "153.6", "250905", "15.2", "E", "A"} | ||
m, _ := Parse(raw) | ||
assert.EqualValues(t, expectedFields, m.GetSentence().Fields, "Got '%q', expected '%q'", m.GetSentence().Fields, expectedFields) | ||
} | ||
|
||
func TestGoodSentenceType(t *testing.T) { | ||
raw := "$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0C" | ||
expected := "GPRMC" | ||
m, _ := Parse(raw) | ||
assert.Equal(t, expected, m.GetSentence().Type, "Got '%s', expected '%s'", m.GetSentence().Type, expected) | ||
raw := "$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0C" | ||
expected := "GPRMC" | ||
m, _ := Parse(raw) | ||
assert.Equal(t, expected, m.GetSentence().Type, "Got '%s', expected '%s'", m.GetSentence().Type, expected) | ||
} | ||
|
||
func TestGoodRawSentence(t *testing.T) { | ||
raw := "$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0C" | ||
m, _ := Parse(raw) | ||
assert.Equal(t, raw, m.GetSentence().Raw, "Bad raw sentence") | ||
raw := "$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0C" | ||
m, _ := Parse(raw) | ||
assert.Equal(t, raw, m.GetSentence().Raw, "Bad raw sentence") | ||
} | ||
|
||
func TestReturnValues(t *testing.T) { | ||
// Ensure Parse returns errors when appropriate. | ||
result, err := Parse("$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0A") | ||
assert.Nil(t, result, "Result should be nil") | ||
assert.NotNil(t, err, "Err should be an error") | ||
assert.Equal(t, "Sentence checksum mismatch [0C != 0A]", err.Error(), "Error sentence mismatch") | ||
// Ensure Parse returns errors when appropriate. | ||
result, err := Parse("$GPRMC,235236,A,3925.9479,N,11945.9211,W,44.7,153.6,250905,15.2,E,A*0A") | ||
assert.Nil(t, result, "Result should be nil") | ||
assert.NotNil(t, err, "Err should be an error") | ||
assert.Equal(t, "Sentence checksum mismatch [0C != 0A]", err.Error(), "Error sentence mismatch") | ||
} |