www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

auto-render-spec.js (8786B)


      1 /* global beforeEach: false */
      2 /* global jasmine: false */
      3 /* global expect: false */
      4 /* global it: false */
      5 /* global describe: false */
      6 
      7 const splitAtDelimiters = require("./splitAtDelimiters");
      8 
      9 beforeEach(function() {
     10     jasmine.addMatchers({
     11         toSplitInto: function() {
     12             return {
     13                 compare: function(actual, left, right, result) {
     14                     const message = {
     15                         pass: true,
     16                         message: "'" + actual + "' split correctly",
     17                     };
     18 
     19                     const startData = [{type: "text", data: actual}];
     20 
     21                     const split =
     22                         splitAtDelimiters(startData, left, right, false);
     23 
     24                     if (split.length !== result.length) {
     25                         message.pass = false;
     26                         message.message = "Different number of splits: " +
     27                             split.length + " vs. " + result.length + " (" +
     28                             JSON.stringify(split) + " vs. " +
     29                             JSON.stringify(result) + ")";
     30                         return message;
     31                     }
     32 
     33                     for (let i = 0; i < split.length; i++) {
     34                         const real = split[i];
     35                         const correct = result[i];
     36 
     37                         let good = true;
     38                         let diff;
     39 
     40                         if (real.type !== correct.type) {
     41                             good = false;
     42                             diff = "type";
     43                         } else if (real.data !== correct.data) {
     44                             good = false;
     45                             diff = "data";
     46                         } else if (real.display !== correct.display) {
     47                             good = false;
     48                             diff = "display";
     49                         }
     50 
     51                         if (!good) {
     52                             message.pass = false;
     53                             message.message = "Difference at split " +
     54                                 (i + 1) + ": " + JSON.stringify(real) +
     55                                 " vs. " + JSON.stringify(correct) +
     56                                 " (" + diff + " differs)";
     57                             break;
     58                         }
     59                     }
     60 
     61                     return message;
     62                 },
     63             };
     64         },
     65     });
     66 });
     67 
     68 describe("A delimiter splitter", function() {
     69     it("doesn't split when there are no delimiters", function() {
     70         expect("hello").toSplitInto("(", ")", [{type: "text", data: "hello"}]);
     71     });
     72 
     73     it("doesn't create a math node with only one left delimiter", function() {
     74         expect("hello ( world").toSplitInto(
     75             "(", ")",
     76             [
     77                 {type: "text", data: "hello "},
     78                 {type: "text", data: "( world"},
     79             ]);
     80     });
     81 
     82     it("doesn't split when there's only a right delimiter", function() {
     83         expect("hello ) world").toSplitInto(
     84             "(", ")",
     85             [
     86                 {type: "text", data: "hello ) world"},
     87             ]);
     88     });
     89 
     90     it("splits when there are both delimiters", function() {
     91         expect("hello ( world ) boo").toSplitInto(
     92             "(", ")",
     93             [
     94                 {type: "text", data: "hello "},
     95                 {type: "math", data: " world ",
     96                     rawData: "( world )", display: false},
     97                 {type: "text", data: " boo"},
     98             ]);
     99     });
    100 
    101     it("splits on multi-character delimiters", function() {
    102         expect("hello [[ world ]] boo").toSplitInto(
    103             "[[", "]]",
    104             [
    105                 {type: "text", data: "hello "},
    106                 {type: "math", data: " world ",
    107                     rawData: "[[ world ]]", display: false},
    108                 {type: "text", data: " boo"},
    109             ]);
    110     });
    111 
    112     it("splits mutliple times", function() {
    113         expect("hello ( world ) boo ( more ) stuff").toSplitInto(
    114             "(", ")",
    115             [
    116                 {type: "text", data: "hello "},
    117                 {type: "math", data: " world ",
    118                     rawData: "( world )", display: false},
    119                 {type: "text", data: " boo "},
    120                 {type: "math", data: " more ",
    121                     rawData: "( more )", display: false},
    122                 {type: "text", data: " stuff"},
    123             ]);
    124     });
    125 
    126     it("leaves the ending when there's only a left delimiter", function() {
    127         expect("hello ( world ) boo ( left").toSplitInto(
    128             "(", ")",
    129             [
    130                 {type: "text", data: "hello "},
    131                 {type: "math", data: " world ",
    132                     rawData: "( world )", display: false},
    133                 {type: "text", data: " boo "},
    134                 {type: "text", data: "( left"},
    135             ]);
    136     });
    137 
    138     it("doesn't split when close delimiters are in {}s", function() {
    139         expect("hello ( world { ) } ) boo").toSplitInto(
    140             "(", ")",
    141             [
    142                 {type: "text", data: "hello "},
    143                 {type: "math", data: " world { ) } ",
    144                     rawData: "( world { ) } )", display: false},
    145                 {type: "text", data: " boo"},
    146             ]);
    147 
    148         expect("hello ( world { { } ) } ) boo").toSplitInto(
    149             "(", ")",
    150             [
    151                 {type: "text", data: "hello "},
    152                 {type: "math", data: " world { { } ) } ",
    153                     rawData: "( world { { } ) } )", display: false},
    154                 {type: "text", data: " boo"},
    155             ]);
    156     });
    157 
    158     it("doesn't split at escaped delimiters", function() {
    159         expect("hello ( world \\) ) boo").toSplitInto(
    160             "(", ")",
    161             [
    162                 {type: "text", data: "hello "},
    163                 {type: "math", data: " world \\) ",
    164                     rawData: "( world \\) )", display: false},
    165                 {type: "text", data: " boo"},
    166             ]);
    167 
    168         /* TODO(emily): make this work maybe?
    169         expect("hello \\( ( world ) boo").toSplitInto(
    170             "(", ")",
    171             [
    172                 {type: "text", data: "hello \\( "},
    173                 {type: "math", data: " world ",
    174                     rawData: "( world )", display: false},
    175                 {type: "text", data: " boo"},
    176             ]);
    177         */
    178     });
    179 
    180     it("splits when the right and left delimiters are the same", function() {
    181         expect("hello $ world $ boo").toSplitInto(
    182             "$", "$",
    183             [
    184                 {type: "text", data: "hello "},
    185                 {type: "math", data: " world ",
    186                     rawData: "$ world $", display: false},
    187                 {type: "text", data: " boo"},
    188             ]);
    189     });
    190 
    191     it("remembers which delimiters are display-mode", function() {
    192         const startData = [{type: "text", data: "hello ( world ) boo"}];
    193 
    194         expect(splitAtDelimiters(startData, "(", ")", true)).toEqual(
    195             [
    196                 {type: "text", data: "hello "},
    197                 {type: "math", data: " world ",
    198                     rawData: "( world )", display: true},
    199                 {type: "text", data: " boo"},
    200             ]);
    201     });
    202 
    203     it("works with more than one start datum", function() {
    204         const startData = [
    205             {type: "text", data: "hello ( world ) boo"},
    206             {type: "math", data: "math", rawData: "(math)", display: true},
    207             {type: "text", data: "hello ( world ) boo"},
    208         ];
    209 
    210         expect(splitAtDelimiters(startData, "(", ")", false)).toEqual(
    211             [
    212                 {type: "text", data: "hello "},
    213                 {type: "math", data: " world ",
    214                     rawData: "( world )", display: false},
    215                 {type: "text", data: " boo"},
    216                 {type: "math", data: "math", rawData: "(math)", display: true},
    217                 {type: "text", data: "hello "},
    218                 {type: "math", data: " world ",
    219                     rawData: "( world )", display: false},
    220                 {type: "text", data: " boo"},
    221             ]);
    222     });
    223 
    224     it("doesn't do splitting inside of math nodes", function() {
    225         const startData = [
    226             {type: "text", data: "hello ( world ) boo"},
    227             {type: "math", data: "hello ( world ) boo",
    228                 rawData: "(hello ( world ) boo)", display: true},
    229         ];
    230 
    231         expect(splitAtDelimiters(startData, "(", ")", false)).toEqual(
    232             [
    233                 {type: "text", data: "hello "},
    234                 {type: "math", data: " world ",
    235                     rawData: "( world )", display: false},
    236                 {type: "text", data: " boo"},
    237                 {type: "math", data: "hello ( world ) boo",
    238                     rawData: "(hello ( world ) boo)", display: true},
    239             ]);
    240     });
    241 });