var text = use('text')

log.console("Testing text module...")
log.console("")

// Test array to text conversion
log.console("== Testing array to text conversion ==")

// Basic array concatenation
var arr1 = ["Hello", " ", "World"]
var result1 = text(arr1)
log.console("text(['Hello', ' ', 'World']) = '" + result1 + "'")
log.console("Expected: 'Hello World'")
log.console("Passed: " + (result1 == "Hello World"))
log.console("")

// Array with separator
var arr2 = ["one", "two", "three"]
var result2 = text(arr2, ", ")
log.console("text(['one', 'two', 'three'], ', ') = '" + result2 + "'")
log.console("Expected: 'one, two, three'")
log.console("Passed: " + (result2 == "one, two, three"))
log.console("")

// Unicode codepoints
var arr3 = [72, 101, 108, 108, 111]
var result3 = text(arr3)
log.console("text([72, 101, 108, 108, 111]) = '" + result3 + "'")
log.console("Expected: 'Hello'")
log.console("Passed: " + (result3 == "Hello"))
log.console("")

// Mixed array with text and codepoints
var arr4 = ["Hi", 32, "there", 33]
var result4 = text(arr4)
log.console("text(['Hi', 32, 'there', 33]) = '" + result4 + "'")
log.console("Expected: 'Hi there!'")
log.console("Passed: " + (result4 == "Hi there!"))
log.console("")

// Test number to text conversion with radix
log.console("== Testing number to text with radix ==")

var tests_radix = [
  {num: 12, radix: 10, expected: "12"},
  {num: 12, radix: 8, expected: "14"},
  {num: 12, radix: 16, expected: "c"},
  {num: 12, radix: 2, expected: "1100"},
  {num: 12, radix: 32, expected: "c"},
  {num: 255, radix: 16, expected: "ff"},
  {num: -42, radix: 10, expected: "-42"},
  {num: 100, radix: 36, expected: "2s"}
]

for (var i = 0; i < tests_radix.length; i++) {
  var test = tests_radix[i]
  var result = text(test.num, test.radix)
  log.console("text(" + test.num + ", " + test.radix + ") = '" + result + "'")
  log.console("Expected: '" + test.expected + "'")
  log.console("Passed: " + (result == test.expected))
}
log.console("")

// Test formatted number conversion
log.console("== Testing formatted number conversion ==")

var num = 123456789.1
var format_tests = [
  {fmt: "n", expected: "123456789.1"},
  {fmt: "3s4", expected: "123 456 789.1000"},
  {fmt: "s", expected: "123 456 789.1"},
  {fmt: "d2", expected: "123,456,789.10"},
  {fmt: "4d0", expected: "1,2345,6789.1"},
  {fmt: "e", expected: "1.234567891e+8"},
  {fmt: "e4", expected: "1.2346e+8"},
  {fmt: "i", expected: "123456789"},
  {fmt: "8b", expected: "111_01011011_11001101_00010101"},
  {fmt: "o", expected: "726746425"},
  {fmt: "h", expected: "75BCD15"},
  {fmt: "t", expected: "3NQK8N"}
]

for (var i = 0; i < format_tests.length; i++) {
  var test = format_tests[i]
  var result = text(num, test.fmt)
  log.console("text(" + num + ", '" + test.fmt + "') = '" + result + "'")
  log.console("Expected: '" + test.expected + "'")
  log.console("Passed: " + (result == test.expected))
}
log.console("")

// Test integer formatting
log.console("== Testing integer formatting ==")

var int_tests = [
  {num: 12, fmt: "4b8", expected: "0000_1100"},
  {num: 12, fmt: "o3", expected: "014"},
  {num: 12, fmt: "h4", expected: "000C"},
  {num: 12, fmt: "t2", expected: "0C"},
  {num: -15, fmt: "h", expected: "-F"},
  {num: 0, fmt: "b", expected: "0"}
]

for (var i = 0; i < int_tests.length; i++) {
  var test = int_tests[i]
  var result = text(test.num, test.fmt)
  log.console("text(" + test.num + ", '" + test.fmt + "') = '" + result + "'")
  log.console("Expected: '" + test.expected + "'")
  log.console("Passed: " + (result == test.expected))
}
log.console("")

// Test text substring operations
log.console("== Testing text substring operations ==")

var str = "miskatonic"
var substr_tests = [
  {from: 0, to: 3, expected: "mis"},
  {from: 3, to: 6, expected: "kat"},
  {from: 5, to: null, expected: "tonic"},
  {from: 0, to: -4, expected: "miskat"},
  {from: -3, to: null, expected: "nic"},
  {from: 0, to: 0, expected: ""},
  {from: 10, to: null, expected: ""},
  {from: 11, to: null, expected: null},
  {from: 2, to: 1, expected: null}
]

for (var i = 0; i < substr_tests.length; i++) {
  var test = substr_tests[i]
  var result = test.to == null ? text(str, test.from) : text(str, test.from, test.to)
  var args = test.to == null ? test.from : test.from + ", " + test.to
  log.console("text('" + str + "', " + args + ") = " + (result == null ? "null" : "'" + result + "'"))
  log.console("Expected: " + (test.expected == null ? "null" : "'" + test.expected + "'"))
  log.console("Passed: " + (result == test.expected))
}
log.console("")

// Test edge cases
log.console("== Testing edge cases ==")

// Empty array
var empty_result = text([])
log.console("text([]) = '" + empty_result + "'")
log.console("Passed: " + (empty_result == ""))

// Single element array
var single_result = text([42])
log.console("text([42]) = '" + single_result + "'")
log.console("Passed: " + (single_result == "42"))

// Text identity
var text_result = text("hello")
log.console("text('hello') = '" + text_result + "'")
log.console("Passed: " + (text_result == "hello"))

// Invalid format
var invalid_result = text(123, "xyz")
log.console("text(123, 'xyz') = " + invalid_result)
log.console("Passed: " + (invalid_result == null))

// Very small numbers with 'n' format
var tiny = 0.0000001
var tiny_result = text(tiny, "n")
log.console("text(0.0000001, 'n') = '" + tiny_result + "'")
log.console("Should use scientific notation: " + (tiny_result.indexOf('e') > -1))

// Very large numbers with 'n' format  
var huge = 1e22
var huge_result = text(huge, "n")
log.console("text(1e22, 'n') = '" + huge_result + "'")
log.console("Should use scientific notation: " + (huge_result.indexOf('e') > -1))

log.console("")

// Summary
log.console("== Test Summary ==")
log.console("All major test categories completed.")
log.console("The text module provides:")
log.console("- Array to text conversion with Unicode support")
log.console("- Number formatting with multiple radix options")
log.console("- Advanced number formatting with real and integer styles")
log.console("- Text substring operations with negative indexing")

$_.stop()