1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00
otto/string_test.go
2013-07-14 14:35:50 -07:00

313 lines
7.7 KiB
Go

package otto
import (
. "./terst"
"testing"
)
func TestString(t *testing.T) {
Terst(t)
test := runTest()
test(`
abc = (new String("xyzzy")).length;
def = new String().length;
ghi = new String("Nothing happens.").length;
`)
test("abc", "5")
test("def", "0")
test("ghi", "16")
test(`"".length`, "0")
test(`"a\uFFFFbc".length`, "4")
test(`String(+0)`, "0")
test(`String(-0)`, "0")
test(`""+-0`, "0")
test(`
var abc = Object.getOwnPropertyDescriptor(String, "prototype");
[ [ typeof String.prototype ],
[ abc.writable, abc.enumerable, abc.configurable ] ];
`, "object,false,false,false")
}
func TestString_charAt(t *testing.T) {
Terst(t)
test := runTest()
test(`
abc = "xyzzy".charAt(0)
def = "xyzzy".charAt(11)
`)
test("abc", "x")
test("def", "")
}
func TestString_charCodeAt(t *testing.T) {
Terst(t)
test := runTest()
test(`
abc = "xyzzy".charCodeAt(0)
def = "xyzzy".charCodeAt(11)
`)
test("abc", "120")
test("def", "NaN")
}
func TestString_fromCharCode(t *testing.T) {
Terst(t)
test := runTest()
test(`String.fromCharCode()`, "")
test(`String.fromCharCode(88, 121, 122, 122, 121)`, "Xyzzy")
test(`String.fromCharCode("88", 121, 122, 122.05, 121)`, "Xyzzy")
test(`String.fromCharCode("88", 121, 122, NaN, 121)`, "Xyz\x00y")
test(`String.fromCharCode("0x21")`, "!")
test(`String.fromCharCode(-1).charCodeAt(0)`, "65535")
test(`String.fromCharCode(65535).charCodeAt(0)`, "65535")
test(`String.fromCharCode(65534).charCodeAt(0)`, "65534")
test(`String.fromCharCode(4294967295).charCodeAt(0)`, "65535")
test(`String.fromCharCode(4294967294).charCodeAt(0)`, "65534")
test(`String.fromCharCode(0x0024) === "$"`, "true")
}
func TestString_concat(t *testing.T) {
Terst(t)
test := runTest()
test(`"".concat()`, "")
test(`"".concat("abc", "def")`, "abcdef")
test(`"".concat("abc", undefined, "def")`, "abcundefineddef")
}
func TestString_indexOf(t *testing.T) {
Terst(t)
test := runTest()
test(`"".indexOf("")`, "0")
test(`"".indexOf("", 11)`, "0")
test(`"abc".indexOf("")`, "0")
test(`"abc".indexOf("", 11)`, "3")
test(`"abc".indexOf("a")`, "0")
test(`"abc".indexOf("bc")`, "1")
test(`"abc".indexOf("bc", 11)`, "-1")
test(`"$$abcdabcd".indexOf("ab", function(){return -Infinity;}())`, "2")
test(`"$$abcdabcd".indexOf("ab", function(){return NaN;}())`, "2")
}
func TestString_lastIndexOf(t *testing.T) {
Terst(t)
test := runTest()
test(`"".lastIndexOf("")`, "0")
test(`"".lastIndexOf("", 11)`, "0")
test(`"abc".lastIndexOf("")`, "3")
test(`"abc".lastIndexOf("", 11)`, "3")
test(`"abc".lastIndexOf("a")`, "0")
test(`"abc".lastIndexOf("bc")`, "1")
test(`"abc".lastIndexOf("bc", 11)`, "1")
test(`"abc".lastIndexOf("bc", 0)`, "-1")
}
func TestString_match(t *testing.T) {
Terst(t)
test := runTest()
test(`"abc____abc_abc___".match(/__abc/)`, "__abc")
test(`"abc___abc_abc__abc__abc".match(/abc/g)`, "abc,abc,abc,abc,abc")
test(`"abc____abc_abc___".match(/__abc/g)`, "__abc")
test(`
abc = /abc/g
"abc___abc_abc__abc__abc".match(abc)
`, "abc,abc,abc,abc,abc")
test(`abc.lastIndex`, "23")
}
func TestString_replace(t *testing.T) {
Terst(t)
test := runTest()
test(`"abc_abc".replace(/abc/, "$&123")`, "abc123_abc")
test(`"abc_abc".replace(/abc/g, "$&123")`, "abc123_abc123")
test(`"abc_abc_".replace(/abc/g, "$&123")`, "abc123_abc123_")
test(`"_abc_abc_".replace(/abc/g, "$&123")`, "_abc123_abc123_")
test(`"abc".replace(/abc/, "$&123")`, "abc123")
test(`"abc_".replace(/abc/, "$&123")`, "abc123_")
test("\"^abc$\".replace(/abc/, \"$`def\")", "^^def$")
test("\"^abc$\".replace(/abc/, \"def$`\")", "^def^$")
test(`"_abc_abd_".replace(/ab(c|d)/g, "$1")`, "_c_d_")
test(`
"_abc_abd_".replace(/ab(c|d)/g, function(){
})
`, "_undefined_undefined_")
test(`"b".replace(/(a)?(b)?/, "_$1_")`, "__")
test(`
"b".replace(/(a)?(b)?/, function(a, b, c, d, e, f){
return [a, b, c, d, e, f]
})
`, "b,,b,0,b,")
}
func TestString_search(t *testing.T) {
Terst(t)
test := runTest()
test(`"abc".search(/abc/)`, "0")
test(`"abc".search(/def/)`, "-1")
test(`"abc".search(/c$/)`, "2")
test(`"abc".search(/$/)`, "3")
}
func TestString_split(t *testing.T) {
Terst(t)
test := runTest()
test(`"abc".split("", 1)`, "a")
test(`"abc".split("", 2)`, "a,b")
test(`"abc".split("", 3)`, "a,b,c")
test(`"abc".split("", 4)`, "a,b,c")
test(`"abc".split("", 11)`, "a,b,c")
test(`"abc".split("", 0)`, "")
test(`"abc".split("")`, "a,b,c")
test(`"abc".split(undefined)`, "abc")
test(`"__1__3_1__2__".split("_")`, ",,1,,3,1,,2,,")
test(`"__1__3_1__2__".split(/_/)`, ",,1,,3,1,,2,,")
test(`"ab".split(/a*/)`, ",b")
test(`_ = "A<B>bold</B>and<CODE>coded</CODE>".split(/<(\/)?([^<>]+)>/)`, "A,,B,bold,/,B,and,,CODE,coded,/,CODE,")
test(`_.length`, "13")
test(`_[1] === undefined`, "true")
test(`_[12] === ""`, "true")
}
func TestString_slice(t *testing.T) {
Terst(t)
test := runTest()
test(`"abc".slice()`, "abc")
test(`"abc".slice(0)`, "abc")
test(`"abc".slice(0,11)`, "abc")
test(`"abc".slice(0,-1)`, "ab")
test(`"abc".slice(-1,11)`, "c")
test(`abc = "abc"; abc.slice(abc.length+1, 0)`, "")
}
func TestString_substring(t *testing.T) {
Terst(t)
test := runTest()
test(`"abc".substring()`, "abc")
test(`"abc".substring(0)`, "abc")
test(`"abc".substring(0,11)`, "abc")
test(`"abc".substring(11,0)`, "abc")
test(`"abc".substring(0,-1)`, "")
test(`"abc".substring(-1,11)`, "abc")
test(`"abc".substring(11,1)`, "bc")
test(`"abc".substring(1)`, "bc")
test(`"abc".substring(Infinity, Infinity)`, "")
}
func TestString_toCase(t *testing.T) {
Terst(t)
test := runTest()
test(`"abc".toLowerCase()`, "abc")
test(`"ABC".toLowerCase()`, "abc")
test(`"abc".toLocaleLowerCase()`, "abc")
test(`"ABC".toLocaleLowerCase()`, "abc")
test(`"abc".toUpperCase()`, "ABC")
test(`"ABC".toUpperCase()`, "ABC")
}
func Test_floatToString(t *testing.T) {
Terst(t)
test := runTest()
test(`String(-1234567890)`, "-1234567890")
test(`-+String(-(-1234567890))`, "-1234567890")
test(`String(-1e128)`, "-1e+128")
test(`String(0.12345)`, "0.12345")
test(`String(-0.00000012345)`, "-1.2345e-7")
test(`String(0.0000012345)`, "0.0000012345")
test(`String(1000000000000000000000)`, "1e+21")
test(`String(1e21)`, "1e+21")
test(`String(1E21)`, "1e+21")
test(`String(-1000000000000000000000)`, "-1e+21")
test(`String(-1e21)`, "-1e+21")
test(`String(-1E21)`, "-1e+21")
test(`String(0.0000001)`, "1e-7")
test(`String(1e-7)`, "1e-7")
test(`String(1E-7)`, "1e-7")
test(`String(-0.0000001)`, "-1e-7")
test(`String(-1e-7)`, "-1e-7")
test(`String(-1E-7)`, "-1e-7")
}
func TestString_indexing(t *testing.T) {
Terst(t)
test := runTest()
// Actually a test of stringToArrayIndex, under the hood.
test(`
abc = new String("abc");
index = Math.pow(2, 32);
[ abc.length, abc[index], abc[index+1], abc[index+2], abc[index+3] ];
`, "3,,,,")
}
func TestString_trim(t *testing.T) {
Terst(t)
test := runTest()
test(`' \n abc \t \n'.trim();`, "abc")
test(`" abc\u000B".trim()`, "abc")
test(`"abc ".trim()`, "abc")
test(`
var a = "\u180Eabc \u000B "
var b = a.trim()
a.length + b.length
`, "10")
}
func TestString_trimLeft(t *testing.T) {
Terst(t)
test := runTest()
test(`" abc\u000B".trimLeft()`, "abc\u000B")
test(`"abc ".trimLeft()`, "abc ")
test(`
var a = "\u180Eabc \u000B "
var b = a.trimLeft()
a.length + b.length
`, "13")
}
func TestString_trimRight(t *testing.T) {
Terst(t)
test := runTest()
test(`" abc\u000B".trimRight()`, " abc")
test(`" abc ".trimRight()`, " abc")
test(`
var a = "\u180Eabc \u000B "
var b = a.trimRight()
a.length + b.length
`, "11")
}
func TestString_localeCompare(t *testing.T) {
Terst(t)
test := runTest()
test(`'a'.localeCompare('c');`, "-1")
test(`'c'.localeCompare('a');`, "1")
test(`'a'.localeCompare('a');`, "0")
}