github.com/ovsinc/prototool@v1.3.0/internal/text/text_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package text 22 23 import ( 24 "bytes" 25 "testing" 26 "text/scanner" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestFailureString(t *testing.T) { 32 assert.Equal(t, "<input>:1:1:hello", newTestFailure("", 0, 0, "", "hello").String()) 33 assert.Equal(t, "<input>:1:2:hello", newTestFailure("", 0, 2, "", "hello").String()) 34 assert.Equal(t, "<input>:2:2:hello", newTestFailure("", 2, 2, "", "hello").String()) 35 assert.Equal(t, "foo:2:2:hello", newTestFailure("foo", 2, 2, "", "hello").String()) 36 assert.Equal(t, "foo:2:2:BAR hello", newTestFailure("foo", 2, 2, "BAR", "hello").String()) 37 } 38 39 func TestFailureFprintln(t *testing.T) { 40 testFailureFprintln(t, "2:1:<input>:BAR", newTestFailure("", 0, 2, "BAR", "hello"), 41 FailureFieldColumn, 42 FailureFieldLine, 43 FailureFieldFilename, 44 FailureFieldID, 45 ) 46 } 47 48 func testFailureFprintln(t *testing.T, expected string, failure *Failure, failureFields ...FailureField) { 49 buffer := bytes.NewBuffer(nil) 50 assert.NoError(t, failure.Fprintln(buffer, failureFields...)) 51 assert.Equal(t, expected+"\n", buffer.String()) 52 } 53 54 func TestParseColonSeparatedFailureFields(t *testing.T) { 55 testParseColonSeparatedFailureFields(t, "", false, DefaultFailureFields...) 56 testParseColonSeparatedFailureFields(t, "filename", false, FailureFieldFilename) 57 testParseColonSeparatedFailureFields(t, "filename:id", false, FailureFieldFilename, FailureFieldID) 58 testParseColonSeparatedFailureFields(t, ":", true) 59 testParseColonSeparatedFailureFields(t, ":filename:id", true) 60 testParseColonSeparatedFailureFields(t, "filename:id:", true) 61 testParseColonSeparatedFailureFields(t, "filename:id2", true) 62 } 63 64 func testParseColonSeparatedFailureFields(t *testing.T, input string, expectError bool, expectedFailureFields ...FailureField) { 65 failureFields, err := ParseColonSeparatedFailureFields(input) 66 if expectError { 67 assert.Error(t, err) 68 } else { 69 assert.NoError(t, err) 70 assert.Equal(t, expectedFailureFields, failureFields) 71 } 72 } 73 74 func TestSortFailures(t *testing.T) { 75 failures := []*Failure{ 76 nil, 77 newTestFailure("foo", 3, 3, "BAT", "mello"), 78 newTestFailure("bar", 3, 3, "BAT", "mello"), 79 newTestFailure("foo", 3, 3, "BAT", "hello"), 80 newTestFailure("foo", 3, 3, "", "hello"), 81 newTestFailure("foo", 2, 3, "", "hello"), 82 newTestFailure("foo", 2, 2, "", "hello"), 83 newTestFailure("foo", 2, 0, "", "hello"), 84 newTestFailure("foo", 3, 3, "BAT", "mello"), 85 newTestFailure("foo", 3, 3, "", "hello"), 86 newTestFailure("foo", 0, 0, "", "hello"), 87 newTestFailure("", 0, 0, "", "hello"), 88 nil, 89 nil, 90 newTestFailure("foo", 3, 3, "BAT", "mello"), 91 newTestFailure("foo", 3, 3, "BAT", "hello"), 92 newTestFailure("foo", 3, 3, "BAR", "hello"), 93 newTestFailure("foo", 2, 3, "", "hello"), 94 newTestFailure("foo", 2, 4, "", "hello"), 95 newTestFailure("foo", 2, 2, "", "hello"), 96 newTestFailure("foo", 3, 3, "BAR", "hello"), 97 newTestFailure("foo", 2, 0, "", "hello"), 98 newTestFailure("foo", 0, 0, "", "hello"), 99 newTestFailure("", 0, 0, "", "hello"), 100 nil, 101 } 102 SortFailures(failures) 103 assert.Equal( 104 t, 105 []*Failure{ 106 nil, 107 nil, 108 nil, 109 nil, 110 newTestFailure("", 0, 0, "", "hello"), 111 newTestFailure("", 0, 0, "", "hello"), 112 newTestFailure("bar", 3, 3, "BAT", "mello"), 113 newTestFailure("foo", 0, 0, "", "hello"), 114 newTestFailure("foo", 0, 0, "", "hello"), 115 newTestFailure("foo", 2, 0, "", "hello"), 116 newTestFailure("foo", 2, 0, "", "hello"), 117 newTestFailure("foo", 2, 2, "", "hello"), 118 newTestFailure("foo", 2, 2, "", "hello"), 119 newTestFailure("foo", 2, 3, "", "hello"), 120 newTestFailure("foo", 2, 3, "", "hello"), 121 newTestFailure("foo", 2, 4, "", "hello"), 122 newTestFailure("foo", 3, 3, "", "hello"), 123 newTestFailure("foo", 3, 3, "", "hello"), 124 newTestFailure("foo", 3, 3, "BAR", "hello"), 125 newTestFailure("foo", 3, 3, "BAR", "hello"), 126 newTestFailure("foo", 3, 3, "BAT", "hello"), 127 newTestFailure("foo", 3, 3, "BAT", "hello"), 128 newTestFailure("foo", 3, 3, "BAT", "mello"), 129 newTestFailure("foo", 3, 3, "BAT", "mello"), 130 newTestFailure("foo", 3, 3, "BAT", "mello"), 131 }, 132 failures, 133 ) 134 } 135 136 func newTestFailure(filename string, line int, column int, id string, message string) *Failure { 137 return NewFailuref(scanner.Position{Filename: filename, Line: line, Column: column}, id, message) 138 }