Skip to content

Commit

Permalink
fix: hash.keys() sometime fail test because map[string]interface{} do…
Browse files Browse the repository at this point in the history
…n't maintain position of their keys
  • Loading branch information
gravataLonga committed Jul 2, 2022
1 parent fc14c41 commit 9ef99b5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 33 deletions.
52 changes: 45 additions & 7 deletions evaluator/hash_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package evaluator

import (
"fmt"
"ninja/object"
"testing"
)
Expand Down Expand Up @@ -227,10 +228,6 @@ func TestHashMethod(t *testing.T) {
`{}.keys()`,
object.Array{Elements: []object.Object{}},
},
{
`{"a": 1, "b": true}.keys()`,
object.Array{Elements: []object.Object{&object.String{Value: "a"}, &object.String{Value: "b"}}},
},
{
`{}.has("a")`,
false,
Expand All @@ -245,10 +242,51 @@ func TestHashMethod(t *testing.T) {
},
}

for _, tt := range tests {
evaluated := testEval(tt.input, t)
for i, tt := range tests {
t.Run(fmt.Sprintf("TestHashMethod_%d", i), func(t *testing.T) {
evaluated := testEval(tt.input, t)

testObjectLiteral(t, evaluated, tt.expected)
testObjectLiteral(t, evaluated, tt.expected)
})
}
}

func TestHashHasKeyMethod(t *testing.T) {
input := `{"a": 1, "b": true}.keys()`
slicesContain := []string{"a", "b"}

contain := func(slice []string, input string) bool {
for _, v := range slice {
if v == input {
return true
}
}
return false
}

evaluated := testEval(input, t)

arr, ok := evaluated.(*object.Array)
if !ok {
t.Fatalf("expect to be an array. Got: %T", evaluated)
}

stringPos1, ok := arr.Elements[0].(*object.String)
if !ok {
t.Fatalf("expect index 0 be a string. Got: %T", evaluated)
}

stringPos2, ok := arr.Elements[0].(*object.String)
if !ok {
t.Fatalf("expect index 0 be a string. Got: %T", evaluated)
}

if !contain(slicesContain, stringPos1.Value) {
t.Fatalf("hash.keys() don't contain %v. Got: %v", slicesContain, stringPos1)
}

if !contain(slicesContain, stringPos2.Value) {
t.Fatalf("hash.keys() don't contain %v. Got: %v", slicesContain, stringPos2)
}
}

Expand Down
49 changes: 25 additions & 24 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,6 @@ func New(in io.Reader) *Lexer {
return l
}

func (l *Lexer) readChar() {

if l.readPosition >= len(l.input) {
l.ch = 0
} else {
l.ch = l.input[l.readPosition]
}

l.position = l.readPosition
l.readPosition += 1

l.keepTrackLineAndCharPosition()

}

func (l *Lexer) keepTrackLineAndCharPosition() {
if l.ch == '\n' {
l.lineNumber += 1
l.characterPositionInLine = 0
} else {
l.characterPositionInLine += 1
}
}

func (l *Lexer) NextToken() token.Token {
var tok token.Token
l.skipWhitespace()
Expand Down Expand Up @@ -165,6 +141,31 @@ func (l *Lexer) NextToken() token.Token {
return tok
}

func (l *Lexer) readChar() {

if l.readPosition >= len(l.input) {
l.ch = 0
} else {
l.ch = l.input[l.readPosition]
}

l.position = l.readPosition
l.readPosition += 1

l.keepTrackLineAndCharPosition()
}

// keepTrackLineAndCharPosition is a method which keep tracking where position of
// pointer of lexer is point at.
func (l *Lexer) keepTrackLineAndCharPosition() {
if l.ch == '\n' {
l.lineNumber += 1
l.characterPositionInLine = 0
} else {
l.characterPositionInLine += 1
}
}

func (l *Lexer) newToken(tokenType token.TokenType, ch []byte) token.Token {
location := token.Location{Line: l.lineNumber + 1, Offset: l.characterPositionInLine}
return token.Token{Type: tokenType, Literal: ch, Location: location}
Expand Down
6 changes: 4 additions & 2 deletions object/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ func hashKeys(keys map[HashKey]HashPair, args ...Object) Object {
if len(args) != 0 {
return NewErrorFormat("hash.keys() expect 0 arguments. Got: %s", InspectArguments(args...))
}
elements := []Object{}
elements := make([]Object, len(keys))
i := 0
for _, pair := range keys {
elements = append(elements, pair.Key)
elements[i] = pair.Key
i++
}

return &Array{Elements: elements}
Expand Down

0 comments on commit 9ef99b5

Please sign in to comment.