mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Improve coverage
This commit is contained in:
@@ -703,3 +703,31 @@ func TestCheck_PointerNode(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "cannot use pointer accessor outside closure")
|
||||
}
|
||||
|
||||
func TestCheck_TypeWeights(t *testing.T) {
|
||||
types := map[string]interface{}{
|
||||
"Uint": uint(1),
|
||||
"Uint8": uint8(2),
|
||||
"Uint16": uint16(3),
|
||||
"Uint32": uint32(4),
|
||||
"Uint64": uint64(5),
|
||||
"Int": int(6),
|
||||
"Int8": int8(7),
|
||||
"Int16": int16(8),
|
||||
"Int32": int32(9),
|
||||
"Int64": int64(10),
|
||||
"Float32": float32(11),
|
||||
"Float64": float64(12),
|
||||
}
|
||||
for a := range types {
|
||||
for b := range types {
|
||||
tree, err := parser.Parse(fmt.Sprintf("%s + %s", a, b))
|
||||
require.NoError(t, err)
|
||||
|
||||
config := conf.New(types)
|
||||
|
||||
_, err = checker.Check(tree, config)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,22 +74,3 @@ func (s *Source) findLineOffset(line int) (int32, bool) {
|
||||
}
|
||||
return -1, false
|
||||
}
|
||||
|
||||
// findLine finds the line that contains the given character offset and
|
||||
// returns the line number and offset of the beginning of that line.
|
||||
// Note that the last line is treated as if it contains all offsets
|
||||
// beyond the end of the actual source.
|
||||
func (s *Source) findLine(characterOffset int32) (int32, int32) {
|
||||
var line int32 = 1
|
||||
for _, lineOffset := range s.lineOffsets {
|
||||
if lineOffset > characterOffset {
|
||||
break
|
||||
} else {
|
||||
line++
|
||||
}
|
||||
}
|
||||
if line == 1 {
|
||||
return line, 0
|
||||
}
|
||||
return line, s.lineOffsets[line-2]
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -52,3 +54,15 @@ func TestStringSource_SnippetSingleLine(t *testing.T) {
|
||||
t.Errorf(unexpectedSnippet, t.Name(), str2, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringSource_MarshalJSON(t *testing.T) {
|
||||
source := NewSource("hello, world")
|
||||
encoded, err := json.Marshal(source)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, `[104,101,108,108,111,44,32,119,111,114,108,100]`, string(encoded))
|
||||
|
||||
decoded := &Source{}
|
||||
err = json.Unmarshal(encoded, decoded)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, source.Content(), decoded.Content())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user