fix tests
This commit is contained in:
249
tests/text.cm
Normal file
249
tests/text.cm
Normal file
@@ -0,0 +1,249 @@
|
||||
var text = use('text')
|
||||
|
||||
return {
|
||||
// Array conversion tests
|
||||
test_array_basic: function() {
|
||||
var arr1 = ["Hello", " ", "World"]
|
||||
var result1 = text(arr1)
|
||||
if (result1 != "Hello World") throw "Basic array concat failed"
|
||||
},
|
||||
|
||||
test_array_separator: function() {
|
||||
var arr2 = ["one", "two", "three"]
|
||||
var result2 = text(arr2, ", ")
|
||||
if (result2 != "one, two, three") throw "Array with separator failed"
|
||||
},
|
||||
|
||||
test_array_codepoints: function() {
|
||||
var arr3 = [72, 101, 108, 108, 111]
|
||||
var result3 = text(arr3)
|
||||
if (result3 != "Hello") throw "Codepoints failed"
|
||||
},
|
||||
|
||||
test_array_mixed: function() {
|
||||
var arr4 = ["Hi", 32, "there", 33]
|
||||
var result4 = text(arr4)
|
||||
if (result4 != "Hi there!") throw "Mixed array failed"
|
||||
},
|
||||
|
||||
// Radix tests
|
||||
test_radix_10: function() {
|
||||
var result = text(12, 10)
|
||||
if (result != "12") throw "Radix 10 failed"
|
||||
},
|
||||
|
||||
test_radix_8: function() {
|
||||
var result = text(12, 8)
|
||||
if (result != "14") throw "Radix 8 failed"
|
||||
},
|
||||
|
||||
test_radix_16: function() {
|
||||
var result = text(12, 16)
|
||||
if (result != "c") throw "Radix 16 failed"
|
||||
},
|
||||
|
||||
test_radix_2: function() {
|
||||
var result = text(12, 2)
|
||||
if (result != "1100") throw "Radix 2 failed"
|
||||
},
|
||||
|
||||
test_radix_32: function() {
|
||||
var result = text(12, 32)
|
||||
if (result != "c") throw "Radix 32 failed"
|
||||
},
|
||||
|
||||
test_radix_hex_large: function() {
|
||||
var result = text(255, 16)
|
||||
if (result != "ff") throw "Radix 16 large failed"
|
||||
},
|
||||
|
||||
test_radix_negative: function() {
|
||||
var result = text(-42, 10)
|
||||
if (result != "-42") throw "Radix negative failed"
|
||||
},
|
||||
|
||||
test_radix_36: function() {
|
||||
var result = text(100, 36)
|
||||
if (result != "2s") throw "Radix 36 failed"
|
||||
},
|
||||
|
||||
// Formatted number tests
|
||||
test_format_n: function() {
|
||||
var result = text(123456789.1, "n")
|
||||
if (result != "123456789.1") throw "Format n failed"
|
||||
},
|
||||
|
||||
test_format_spaced_decimal: function() {
|
||||
var result = text(123456789.1, "3s4")
|
||||
if (result != "123 456 789.1000") throw "Format 3s4 failed"
|
||||
},
|
||||
|
||||
test_format_s: function() {
|
||||
var result = text(123456789.1, "s")
|
||||
if (result != "123 456 789.1") throw "Format s failed"
|
||||
},
|
||||
|
||||
test_format_d2: function() {
|
||||
var result = text(123456789.1, "d2")
|
||||
if (result != "123,456,789.10") throw "Format d2 failed"
|
||||
},
|
||||
|
||||
test_format_4d0: function() {
|
||||
var result = text(123456789.1, "4d0")
|
||||
if (result != "1,2345,6789.1") throw "Format 4d0 failed"
|
||||
},
|
||||
|
||||
test_format_e: function() {
|
||||
var result = text(123456789.1, "e")
|
||||
if (result != "1.234567891e+8") throw "Format e failed"
|
||||
},
|
||||
|
||||
test_format_e4: function() {
|
||||
var result = text(123456789.1, "e4")
|
||||
if (result != "1.2346e+8") throw "Format e4 failed"
|
||||
},
|
||||
|
||||
test_format_i: function() {
|
||||
var result = text(123456789.1, "i")
|
||||
if (result != "123456789") throw "Format i failed"
|
||||
},
|
||||
|
||||
test_format_8b: function() {
|
||||
var result = text(123456789.1, "8b")
|
||||
if (result != "111_01011011_11001101_00010101") throw "Format 8b failed"
|
||||
},
|
||||
|
||||
test_format_o: function() {
|
||||
var result = text(123456789.1, "o")
|
||||
if (result != "726746425") throw "Format o failed"
|
||||
},
|
||||
|
||||
test_format_h: function() {
|
||||
var result = text(123456789.1, "h")
|
||||
if (result != "75BCD15") throw "Format h failed"
|
||||
},
|
||||
|
||||
test_format_t: function() {
|
||||
var result = text(123456789.1, "t")
|
||||
if (result != "3NQK8N") throw "Format t failed"
|
||||
},
|
||||
|
||||
// Integer formatting tests
|
||||
test_int_4b8: function() {
|
||||
var result = text(12, "4b8")
|
||||
if (result != "0000_1100") throw "Int format 4b8 failed"
|
||||
},
|
||||
|
||||
test_int_o3: function() {
|
||||
var result = text(12, "o3")
|
||||
if (result != "014") throw "Int format o3 failed"
|
||||
},
|
||||
|
||||
test_int_h4: function() {
|
||||
var result = text(12, "h4")
|
||||
if (result != "000C") throw "Int format h4 failed"
|
||||
},
|
||||
|
||||
test_int_t2: function() {
|
||||
var result = text(12, "t2")
|
||||
if (result != "0C") throw "Int format t2 failed"
|
||||
},
|
||||
|
||||
test_int_h_negative: function() {
|
||||
var result = text(-15, "h")
|
||||
if (result != "-F") throw "Int format negative h failed"
|
||||
},
|
||||
|
||||
test_int_b_zero: function() {
|
||||
var result = text(0, "b")
|
||||
if (result != "0") throw "Int format zero b failed"
|
||||
},
|
||||
|
||||
// Substring tests
|
||||
test_substr_start: function() {
|
||||
var str = "miskatonic"
|
||||
var result = text(str, 0, 3)
|
||||
if (result != "mis") throw "Substr start failed"
|
||||
},
|
||||
|
||||
test_substr_middle: function() {
|
||||
var str = "miskatonic"
|
||||
var result = text(str, 3, 6)
|
||||
if (result != "kat") throw "Substr middle failed"
|
||||
},
|
||||
|
||||
test_substr_to_end: function() {
|
||||
var str = "miskatonic"
|
||||
var result = text(str, 5, null)
|
||||
if (result != "tonic") throw "Substr to end failed"
|
||||
},
|
||||
|
||||
test_substr_negative_to: function() {
|
||||
var str = "miskatonic"
|
||||
var result = text(str, 0, -4)
|
||||
if (result != "miskat") throw "Substr negative to failed"
|
||||
},
|
||||
|
||||
test_substr_negative_from: function() {
|
||||
var str = "miskatonic"
|
||||
var result = text(str, -3, null)
|
||||
if (result != "nic") throw "Substr negative from failed"
|
||||
},
|
||||
|
||||
test_substr_empty_range: function() {
|
||||
var str = "miskatonic"
|
||||
var result = text(str, 0, 0)
|
||||
if (result != "") throw "Substr empty range failed"
|
||||
},
|
||||
|
||||
test_substr_past_end: function() {
|
||||
var str = "miskatonic"
|
||||
var result = text(str, 10, null)
|
||||
if (result != "") throw "Substr past end failed"
|
||||
},
|
||||
|
||||
test_substr_out_of_bounds: function() {
|
||||
var str = "miskatonic"
|
||||
var result = text(str, 11, null)
|
||||
if (result != null) throw "Substr out of bounds failed"
|
||||
},
|
||||
|
||||
test_substr_invalid_range: function() {
|
||||
var str = "miskatonic"
|
||||
var result = text(str, 2, 1)
|
||||
if (result != null) throw "Substr invalid range failed"
|
||||
},
|
||||
|
||||
// Edge cases
|
||||
test_empty_array: function() {
|
||||
var result = text([])
|
||||
if (result != "") throw "Empty array test failed"
|
||||
},
|
||||
|
||||
test_single_element: function() {
|
||||
var result = text([42])
|
||||
if (result != "42") throw "Single element array test failed"
|
||||
},
|
||||
|
||||
test_identity: function() {
|
||||
var result = text("hello")
|
||||
if (result != "hello") throw "Text identity test failed"
|
||||
},
|
||||
|
||||
test_invalid_format: function() {
|
||||
var result = text(123, "xyz")
|
||||
if (result != null) throw "Invalid format test failed"
|
||||
},
|
||||
|
||||
test_tiny_number: function() {
|
||||
var tiny = 0.0000001
|
||||
var result = text(tiny, "n")
|
||||
if (result.indexOf('e') == -1) throw "Tiny number format failed"
|
||||
},
|
||||
|
||||
test_huge_number: function() {
|
||||
var huge = 1e22
|
||||
var result = text(huge, "n")
|
||||
if (result.indexOf('e') == -1) throw "Huge number format failed"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user