[483] | 1 | define(["doh/main", "../string"], function(doh, string){ |
---|
| 2 | |
---|
| 3 | doh.register("tests.string", |
---|
| 4 | [ |
---|
| 5 | function test_string_pad(t){ |
---|
| 6 | t.is("00001", string.pad("1", 5)); |
---|
| 7 | t.is("000001", string.pad("000001", 5)); |
---|
| 8 | t.is("10000", string.pad("1", 5, null, true)); |
---|
| 9 | }, |
---|
| 10 | |
---|
| 11 | function test_string_substitute(t){ |
---|
| 12 | t.is("File 'foo.html' is not found in directory '/temp'.", |
---|
| 13 | string.substitute( |
---|
| 14 | "File '${0}' is not found in directory '${1}'.", |
---|
| 15 | ["foo.html","/temp"] |
---|
| 16 | ) |
---|
| 17 | ); |
---|
| 18 | t.is("File 'foo.html' is not found in directory '/temp'.", |
---|
| 19 | string.substitute( |
---|
| 20 | "File '${name}' is not found in directory '${info.dir}'.", |
---|
| 21 | { |
---|
| 22 | name: "foo.html", |
---|
| 23 | info: { dir: "/temp" } |
---|
| 24 | } |
---|
| 25 | ) |
---|
| 26 | ); |
---|
| 27 | // Verify that an error is thrown! |
---|
| 28 | t.assertError(Error, string, "substitute", ["${x}", {y:1}]); |
---|
| 29 | }, |
---|
| 30 | |
---|
| 31 | function test_string_substitute_transform(t){ |
---|
| 32 | var getPrefix = function(str){ |
---|
| 33 | // try to figure out the type |
---|
| 34 | var prefix = (str.charAt(0) == "/") ? "directory": "file"; |
---|
| 35 | if(this.____prefix){ |
---|
| 36 | prefix = this.____prefix + prefix; |
---|
| 37 | } |
---|
| 38 | return prefix + " '" + str + "'"; |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | var obj = { |
---|
| 42 | ____prefix: "...", |
---|
| 43 | getPrefix: getPrefix |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | t.is("file 'foo.html' is not found in directory '/temp'.", |
---|
| 47 | string.substitute( |
---|
| 48 | "${0} is not found in ${1}.", |
---|
| 49 | ["foo.html","/temp"], |
---|
| 50 | getPrefix |
---|
| 51 | ) |
---|
| 52 | ); |
---|
| 53 | |
---|
| 54 | t.is("...file 'foo.html' is not found in ...directory '/temp'.", |
---|
| 55 | string.substitute( |
---|
| 56 | "${0} is not found in ${1}.", |
---|
| 57 | ["foo.html","/temp"], |
---|
| 58 | obj.getPrefix, obj |
---|
| 59 | ) |
---|
| 60 | ); |
---|
| 61 | }, |
---|
| 62 | |
---|
| 63 | function test_string_substitute_formatter(t){ |
---|
| 64 | t.is("thinger -- howdy", |
---|
| 65 | string.substitute( |
---|
| 66 | "${0:postfix}", ["thinger"], null, { |
---|
| 67 | postfix: function(value, key){ |
---|
| 68 | return value + " -- howdy"; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | ) |
---|
| 72 | ); |
---|
| 73 | }, |
---|
| 74 | |
---|
| 75 | function test_string_trim(t){ |
---|
| 76 | t.is("astoria", string.trim(" \f\n\r\t astoria ")); |
---|
| 77 | t.is("astoria", string.trim("astoria ")); |
---|
| 78 | t.is("astoria", string.trim(" astoria")); |
---|
| 79 | t.is("astoria", string.trim("astoria")); |
---|
| 80 | t.is("a", string.trim(" a ")); |
---|
| 81 | }, |
---|
| 82 | |
---|
| 83 | function test_string_rep(t){ |
---|
| 84 | t.is("aaaaa", string.rep("a", 5)); |
---|
| 85 | t.is("abababab", string.rep("ab", 4)); |
---|
| 86 | t.is("", string.rep("ab", 0)); |
---|
| 87 | t.is("", string.rep("", 3)); |
---|
| 88 | } |
---|
| 89 | ] |
---|
| 90 | ); |
---|
| 91 | |
---|
| 92 | }); |
---|