MCPcopy Create free account
hub / github.com/dop251/goja / TestPosition

Function TestPosition

parser/parser_test.go:1066–1187  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1064}
1065
1066func TestPosition(t *testing.T) {
1067 tt(t, func() {
1068 parser := newParser("", "// Lorem ipsum")
1069
1070 // Out of range, idx0 (error condition)
1071 is(parser.slice(0, 1), "")
1072 is(parser.slice(0, 10), "")
1073
1074 // Out of range, idx1 (error condition)
1075 is(parser.slice(1, 128), "")
1076
1077 is(parser.str[0:0], "")
1078 is(parser.slice(1, 1), "")
1079
1080 is(parser.str[0:1], "/")
1081 is(parser.slice(1, 2), "/")
1082
1083 is(parser.str[0:14], "// Lorem ipsum")
1084 is(parser.slice(1, 15), "// Lorem ipsum")
1085
1086 parser = newParser("", "(function(){ return 0; })")
1087 program, err := parser.parse()
1088 is(err, nil)
1089
1090 var node ast.Node
1091 node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral)
1092 is(node.Idx0(), file.Idx(2))
1093 is(node.Idx1(), file.Idx(25))
1094 is(parser.slice(node.Idx0(), node.Idx1()), "function(){ return 0; }")
1095 is(parser.slice(node.Idx0(), node.Idx1()+1), "function(){ return 0; })")
1096 is(parser.slice(node.Idx0(), node.Idx1()+2), "")
1097 is(node.(*ast.FunctionLiteral).Source, "function(){ return 0; }")
1098
1099 node = program
1100 is(node.Idx0(), file.Idx(2))
1101 is(node.Idx1(), file.Idx(25))
1102 is(parser.slice(node.Idx0(), node.Idx1()), "function(){ return 0; }")
1103
1104 parser = newParser("", "(function(){ return abc; })")
1105 program, err = parser.parse()
1106 is(err, nil)
1107 node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral)
1108 is(node.(*ast.FunctionLiteral).Source, "function(){ return abc; }")
1109 node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.List[0].(*ast.ReturnStatement)
1110 is(parser.slice(node.Idx0(), node.Idx1()), "return abc")
1111
1112 parser = newParser("", "(function(){ return; })")
1113 program, err = parser.parse()
1114 is(err, nil)
1115 node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.List[0].(*ast.ReturnStatement)
1116 is(parser.slice(node.Idx0(), node.Idx1()), "return")
1117
1118 parser = newParser("", "true ? 1 : 2")
1119 program, err = parser.parse()
1120 is(err, nil)
1121 node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ConditionalExpression)
1122 is(parser.slice(node.Idx0(), node.Idx1()), "true ? 1 : 2")
1123

Callers

nothing calls this directly

Calls 8

Idx0Method · 0.95
Idx1Method · 0.95
IdxTypeAlias · 0.92
ttFunction · 0.85
newParserFunction · 0.85
isFunction · 0.85
sliceMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…