diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/renderkid/test/layout | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/renderkid/test/layout')
-rw-r--r-- | node_modules/renderkid/test/layout/Block.coffee | 312 | ||||
-rw-r--r-- | node_modules/renderkid/test/layout/SpecialString.coffee | 82 |
2 files changed, 394 insertions, 0 deletions
diff --git a/node_modules/renderkid/test/layout/Block.coffee b/node_modules/renderkid/test/layout/Block.coffee new file mode 100644 index 000000000..e5b5ebe38 --- /dev/null +++ b/node_modules/renderkid/test/layout/Block.coffee @@ -0,0 +1,312 @@ +Layout = require '../../src/Layout' +{object} = require 'utila' + +{open, get, conf} = do -> + show = (layout) -> + got = layout.get() + got = got.replace /<[^>]+>/g, '' + + defaultBlockConfig = + linePrependor: options: amount: 2 + + c = (add = {}) -> + object.append defaultBlockConfig, add + + ret = {} + + ret.open = (block, name, top = 0, bottom = 0) -> + config = c + blockPrependor: options: amount: top + blockAppendor: options: amount: bottom + + b = block.openBlock config, name + b.write name + ' | top ' + top + ' bottom ' + bottom + b + + ret.get = (layout) -> + layout.get().replace(/<[^>]+>/g, '') + + ret.conf = (props) -> + config = {} + if props.left? + object.appendOnto config, linePrependor: options: amount: props.left + + if props.right? + object.appendOnto config, lineAppendor: options: amount: props.right + + if props.top? + object.appendOnto config, blockPrependor: options: amount: props.top + + if props.bottom? + object.appendOnto config, blockAppendor: options: amount: props.bottom + + if props.width? + object.appendOnto config, width: props.width + + if props.bullet is yes + object.appendOnto config, linePrependor: options: bullet: {char: '-', alignment: 'left'} + + config + + ret + + +describe "Layout", -> + describe "inline inputs", -> + it "should be merged", -> + l = new Layout + + l.write 'a' + l.write 'b' + + get(l).should.equal 'ab' + + it "should be correctly wrapped", -> + l = new Layout + block = l.openBlock conf width: 20 + block.write '123456789012345678901234567890' + block.close() + get(l).should.equal '12345678901234567890\n1234567890' + + it "should trim from left when wrapping to a new line", -> + l = new Layout + block = l.openBlock conf width: 20 + block.write '12345678901234567890 \t 123456789012345678901' + block.close() + get(l).should.equal '12345678901234567890\n12345678901234567890\n1' + + it "should handle line breaks correctly", -> + l = new Layout + block = l.openBlock conf width: 20 + block.write '\na\n\nb\n' + block.close() + get(l).should.equal '\na\n\nb\n' + + it "should not put extra line breaks when a line is already broken", -> + l = new Layout + block = l.openBlock conf width: 20 + block.write '01234567890123456789\n0123456789' + block.close() + get(l).should.equal '01234567890123456789\n0123456789' + + describe "horizontal margins", -> + it "should account for left margins", -> + l = new Layout + block = l.openBlock conf width: 20, left: 2 + block.write '01' + block.close() + get(l).should.equal ' 01' + + it "should account for right margins", -> + l = new Layout + block = l.openBlock conf width: 20, right: 2 + block.write '01' + block.close() + get(l).should.equal '01 ' + + it "should account for both margins", -> + l = new Layout + block = l.openBlock conf width: 20, right: 2, left: 1 + block.write '01' + block.close() + get(l).should.equal ' 01 ' + + it "should break lines according to left margins", -> + l = new Layout + global.tick = yes + block = l.openBlock conf width: 20, left: 2 + block.write '01234567890123456789' + block.close() + global.tick = no + get(l).should.equal ' 01234567890123456789' + + it "should break lines according to right margins", -> + l = new Layout + block = l.openBlock conf width: 20, right: 2 + block.write '01234567890123456789' + block.close() + get(l).should.equal '01234567890123456789 ' + + it "should break lines according to both margins", -> + l = new Layout + block = l.openBlock conf width: 20, right: 2, left: 1 + block.write '01234567890123456789' + block.close() + get(l).should.equal ' 01234567890123456789 ' + + it "should break lines according to terminal width", -> + l = new Layout terminalWidth: 20 + block = l.openBlock conf right: 2, left: 1 + block.write '01234567890123456789' + block.close() + + # Note: We don't expect ' 01234567890123456 \n 789 ', + # since the first line (' 01234567890123456 ') is a full line + # according to layout.config.terminalWidth and doesn't need + # a break line. + get(l).should.equal ' 01234567890123456 789 ' + + describe "lines and blocks", -> + it "should put one break line between: line, block", -> + l = new Layout + l.write 'a' + l.openBlock().write('b').close() + get(l).should.equal 'a\nb' + + it "should put one break line between: block, line", -> + l = new Layout + l.openBlock().write('a').close() + l.write 'b' + get(l).should.equal 'a\nb' + + it "should put one break line between: line, block, line", -> + l = new Layout + l.write 'a' + l.openBlock().write('b').close() + l.write 'c' + get(l).should.equal 'a\nb\nc' + + it "margin top should work for: line, block", -> + l = new Layout + l.write 'a' + l.openBlock(conf top: 2).write('b').close() + get(l).should.equal 'a\n\n\nb' + + it "margin top should work for: block, line", -> + l = new Layout + l.openBlock(conf top: 1).write('a').close() + l.write 'b' + get(l).should.equal '\na\nb' + + it "margin top should work for: block, line, when block starts with a break", -> + l = new Layout + l.openBlock(conf top: 1).write('\na').close() + l.write 'b' + get(l).should.equal '\n\na\nb' + + it "margin top should work for: line, block, when line ends with a break", -> + l = new Layout + l.write 'a\n' + l.openBlock(conf top: 1).write('b').close() + get(l).should.equal 'a\n\n\nb' + + it "margin top should work for: line, block, when there are two breaks in between", -> + l = new Layout + l.write 'a\n' + l.openBlock(conf top: 1).write('\nb').close() + get(l).should.equal 'a\n\n\n\nb' + + it "margin bottom should work for: line, block", -> + l = new Layout + l.write 'a' + l.openBlock(conf bottom: 1).write('b').close() + get(l).should.equal 'a\nb\n' + + it "margin bottom should work for: block, line", -> + l = new Layout + l.openBlock(conf bottom: 1).write('a').close() + l.write 'b' + get(l).should.equal 'a\n\nb' + + it "margin bottom should work for: block, line, when block ends with a break", -> + l = new Layout + l.openBlock(conf bottom: 1).write('a\n').close() + l.write 'b' + get(l).should.equal 'a\n\n\nb' + + it "margin bottom should work for: block, line, when line starts with a break", -> + l = new Layout + l.openBlock(conf bottom: 1).write('a').close() + l.write '\nb' + get(l).should.equal 'a\n\n\nb' + + it "margin bottom should work for: block, line, when there are two breaks in between", -> + l = new Layout + l.openBlock(conf bottom: 1).write('a\n').close() + l.write '\nb' + get(l).should.equal 'a\n\n\n\nb' + + describe "blocks and blocks", -> + it "should not get extra break lines for full-width lines", -> + l = new Layout + l.openBlock(conf width: 20).write('01234567890123456789').close() + l.openBlock().write('b').close() + get(l).should.equal '01234567890123456789\nb' + + it "should not get extra break lines for full-width lines followed by a margin", -> + l = new Layout + l.openBlock(conf width: 20, bottom: 1).write('01234567890123456789').close() + l.openBlock().write('b').close() + get(l).should.equal '01234567890123456789\n\nb' + + it "a(top: 0, bottom: 0) b(top: 0, bottom: 0)", -> + l = new Layout + l.openBlock().write('a').close() + l.openBlock().write('b').close() + get(l).should.equal 'a\nb' + + it "a(top: 0, bottom: 0) b(top: 1, bottom: 0)", -> + l = new Layout + l.openBlock().write('a').close() + l.openBlock(conf(top: 1)).write('b').close() + get(l).should.equal 'a\n\nb' + + it "a(top: 0, bottom: 1) b(top: 0, bottom: 0)", -> + l = new Layout + l.openBlock(conf(bottom: 1)).write('a').close() + l.openBlock().write('b').close() + get(l).should.equal 'a\n\nb' + + it "a(top: 0, bottom: 1 ) b( top: 1, bottom: 0)", -> + l = new Layout + l.openBlock(conf(bottom: 1)).write('a').close() + l.openBlock(conf(top: 1)).write('b').close() + get(l).should.equal 'a\n\n\nb' + + it "a(top: 0, bottom: 1 br) b(br top: 1, bottom: 0)", -> + l = new Layout + l.openBlock(conf(bottom: 1)).write('a\n').close() + l.openBlock(conf(top: 1)).write('\nb').close() + get(l).should.equal 'a\n\n\n\n\nb' + + it "a(top: 2, bottom: 3 a1-br-a2) b(br-b1-br-br-b2-br top: 2, bottom: 3)", -> + l = new Layout + l.openBlock(conf(top: 2, bottom: 3)).write('a1\na2').close() + l.openBlock(conf(top: 2, bottom: 3)).write('\nb1\n\nb2\n').close() + get(l).should.equal '\n\na1\na2\n\n\n\n\n\n\nb1\n\nb2\n\n\n\n' + + describe "nesting", -> + it "should break one line for nested blocks", -> + l = new Layout + l.write 'a' + b = l.openBlock() + c = b.openBlock().write('c').close() + b.close() + get(l).should.equal 'a\nc' + + it "a(left: 2) > b(top: 2)", -> + l = new Layout + a = l.openBlock(conf(left: 2)) + a.openBlock(conf(top: 2)).write('b').close() + a.close() + get(l).should.equal ' \n \n b' + + it "a(left: 2) > b(bottom: 2)", -> + l = new Layout + a = l.openBlock(conf(left: 2)) + a.openBlock(conf(bottom: 2)).write('b').close() + a.close() + get(l).should.equal ' b\n \n ' + + describe "bullets", -> + it "basic bullet", -> + l = new Layout + l.openBlock(conf(left: 3, bullet: yes)).write('a').close() + get(l).should.equal '- a' + + it "a(left: 3, bullet) > b(top:1)", -> + l = new Layout + a = l.openBlock(conf(left: 3, bullet: yes)) + b = a.openBlock(conf(top: 1)).write('b').close() + a.close() + get(l).should.equal '- \n b'
\ No newline at end of file diff --git a/node_modules/renderkid/test/layout/SpecialString.coffee b/node_modules/renderkid/test/layout/SpecialString.coffee new file mode 100644 index 000000000..acc166fbd --- /dev/null +++ b/node_modules/renderkid/test/layout/SpecialString.coffee @@ -0,0 +1,82 @@ +S = require '../../src/layout/SpecialString' + +describe "SpecialString", -> + describe 'SpecialString()', -> + it 'should return instance', -> + S('s').should.be.instanceOf S + + describe 'length()', -> + it 'should return correct length for normal text', -> + S('hello').length.should.equal 5 + + it 'should return correct length for text containing tabs and tags', -> + S('<a>he<you />l\tlo</a>').length.should.equal 13 + + it "shouldn't count empty tags as tags", -> + S('<>><').length.should.equal 4 + + it "should count length of single tag as 0", -> + S('<html>').length.should.equal 0 + + it "should work correctly with html quoted characters", -> + S(' >< &sp;').length.should.equal 5 + + describe 'splitIn()', -> + it "should work correctly with normal text", -> + S("123456").splitIn(3).should.be.like ['123', '456'] + + it "should work correctly with normal text containing tabs and tags", -> + S("12\t3<hello>456").splitIn(3).should.be.like ['12', '\t', '3<hello>45', '6'] + + it "should not trimLeft all lines when trimLeft is no", -> + S('abc def').splitIn(3).should.be.like ['abc', ' de', 'f'] + + it "should trimLeft all lines when trimLeft is true", -> + S('abc def').splitIn(3, yes).should.be.like ['abc', 'def'] + + describe 'cut()', -> + it "should work correctly with text containing tabs and tags", -> + original = S("12\t3<hello>456") + cut = original.cut(2, 3) + original.str.should.equal '123<hello>456' + cut.str.should.equal '\t' + + it "should trim left when trimLeft is true", -> + original = S ' 132' + cut = original.cut 0, 1, yes + original.str.should.equal '32' + cut.str.should.equal '1' + + it "should be greedy", -> + S("ab<tag>a").cut(0, 2).str.should.equal "ab<tag>" + + describe 'isOnlySpecialChars()', -> + it "should work", -> + S("12\t3<hello>456").isOnlySpecialChars().should.equal no + S("<hello>").isOnlySpecialChars().should.equal yes + + describe 'clone()', -> + it "should return independent instance", -> + a = S('hello') + b = a.clone() + a.str.should.equal b.str + a.should.not.equal b + + describe 'trim()', -> + it "should return an independent instance", -> + s = S('') + s.trim().should.not.equal s + + it 'should return the same string when trim is not required', -> + S('hello').trim().str.should.equal 'hello' + + it 'should return trimmed string', -> + S(' hello').trim().str.should.equal 'hello' + + describe 'trimLeft()', -> + it "should only trim on the left", -> + S(' hello ').trimLeft().str.should.equal 'hello ' + + describe 'trimRight()', -> + it "should only trim on the right", -> + S(' hello ').trimRight().str.should.equal ' hello'
\ No newline at end of file |