]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/npmlog/node_modules/gauge/test/progress-bar.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / npmlog / node_modules / gauge / test / progress-bar.js
1 "use strict"
2 var test = require("tap").test
3 var ProgressBar = require("../progress-bar.js")
4
5 var cursor = []
6 var C
7 var bar = new ProgressBar({theme: ProgressBar.ascii, maxUpdateFrequency: 0}, C = {
8   show: function () {
9     cursor.push(["show"])
10     return C
11   },
12   hide: function () {
13     cursor.push(["hide"])
14     return C
15   },
16   up: function (lines) {
17     cursor.push(["up",lines])
18     return C
19   },
20   horizontalAbsolute: function (col) {
21     cursor.push(["horizontalAbsolute", col])
22     return C
23   },
24   eraseLine: function () {
25     cursor.push(["eraseLine"])
26     return C
27   },
28   write: function (line) {
29     cursor.push(["write", line])
30     return C
31   }
32 })
33
34
35 function isOutput(t, msg, output) {
36   var tests = []
37   for (var ii = 0; ii<output.length; ++ii) {
38     for (var jj = 0; jj<output[ii].length; ++jj) {
39       tests.push({cmd: ii, arg: jj})
40     }
41   }
42   tests.forEach(function(test) {
43     t.is(cursor[test.cmd] ? cursor[test.cmd][test.arg] : null,
44          output[test.cmd][test.arg],
45          msg + ": " + output[test.cmd] + (test.arg ? " arg #"+test.arg : ""))
46   })
47 }
48
49 test("hide", function (t) {
50   t.plan(11)
51   process.stderr.isTTY = false
52   bar.hide()
53   t.is(cursor.length, 0, "We don't progress bar without a tty")
54   cursor = []
55   process.stderr.isTTY = true
56   bar.hide()
57   isOutput(t, "hide while not showing",[
58     ["show"], // cursor
59     ["horizontalAbsolute",0],
60     ["eraseLine"]])
61   cursor = []
62   bar.showing = true
63   bar.hide()
64   isOutput(t, "hide while showing",[
65     ["show"], // cursor
66     ["up", 1],
67     ["horizontalAbsolute",0],
68     ["eraseLine"]])
69 })
70
71 test("renderTemplate", function (t) {
72   t.plan(16)
73   process.stdout.columns = 11
74   var result = bar.renderTemplate(ProgressBar.ascii,[{type: "name"}],{name: "NAME"})
75   t.is(result, "NAME", "name substitution")
76   var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: 0})
77   t.is(result, "----------", "0% bar")
78   var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: 0.5})
79   t.is(result, "#####-----", "50% bar")
80   var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: 1})
81   t.is(result, "##########", "100% bar")
82   var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: -100})
83   t.is(result, "----------", "0% underflow bar")
84   var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: 100})
85   t.is(result, "##########", "100% overflow bar")
86   var result = bar.renderTemplate(ProgressBar.ascii,[{type: "name"},{type: "completionbar"}],{name: "NAME", completed: 0.5})
87   t.is(result, "NAME###---", "name + 50%")
88   var result = bar.renderTemplate(ProgressBar.ascii, ["static"], {})
89   t.is(result, "static", "static text")
90   var result = bar.renderTemplate(ProgressBar.ascii, ["static",{type: "name"}], {name: "NAME"})
91   t.is(result, "staticNAME", "static text + var")
92   var result = bar.renderTemplate(ProgressBar.ascii, ["static",{type: "name", separated: true}], {name: "NAME"})
93   t.is(result, "static NAME ", "pre-separated")
94   var result = bar.renderTemplate(ProgressBar.ascii, [{type: "name", separated: true}, "static"], {name: "NAME"})
95   t.is(result, "NAME static", "post-separated")
96   var result = bar.renderTemplate(ProgressBar.ascii, ["1",{type: "name", separated: true}, "2"], {name: ""})
97   t.is(result, "12", "separated no value")
98   var result = bar.renderTemplate(ProgressBar.ascii, ["1",{type: "name", separated: true}, "2"], {name: "NAME"})
99   t.is(result, "1 NAME 2", "separated value")
100   var result = bar.renderTemplate(ProgressBar.ascii, [{type: "spinner"}], {spinner: 0})
101   t.is(result, "", "No spinner")
102   var result = bar.renderTemplate(ProgressBar.ascii, [{type: "spinner"}], {spinner: 1})
103   t.is(result, "\\", "Spinner 1")
104   var result = bar.renderTemplate(ProgressBar.ascii, [{type: "spinner"}], {spinner: 10})
105   t.is(result, "|", "Spinner 10")
106 })
107
108 test("show & pulse", function (t) {
109   t.plan(23)
110
111   process.stdout.columns = 16
112   cursor = []
113   process.stderr.isTTY = false
114   bar.template[0].length = 6
115   bar.last = new Date(0)
116   bar.show("NAME", 0)
117   t.is(cursor.length, 0, "no tty, no progressbar")
118
119   cursor = []
120   process.stderr.isTTY = true
121   bar.last = new Date(0)
122   bar.show("NAME", 0.1)
123   isOutput(t, "tty, name, completion",
124     [ [ 'hide' ],
125       [ 'horizontalAbsolute', 0 ],
126       [ 'write', 'NAME   |#-----|\n' ],
127       [ 'show' ] ])
128
129   bar.show("S")
130   cursor = []
131   bar.last = new Date(0)
132   bar.pulse()
133   isOutput(t, "pulsed spinner",
134     [ [ 'up', 1 ],
135       [ 'hide' ],
136       [ 'horizontalAbsolute', 0 ],
137       [ 'write', 'S      \\ |----|\n' ],
138       [ 'show' ] ])
139   cursor = []
140   bar.last = new Date(0)
141   bar.pulse("P")
142   isOutput(t, "pulsed spinner with subsection",
143     [ [ 'up', 1 ],
144       [ 'hide' ],
145       [ 'horizontalAbsolute', 0 ],
146       [ 'write', 'S -> P | |----|\n' ],
147       [ 'show' ] ])
148 })
149
150 test("window resizing", function (t) {
151   t.plan(16)
152   process.stderr.isTTY = true
153   process.stdout.columns = 32
154   bar.show("NAME", 0.1)
155   cursor = []
156   bar.last = new Date(0)
157   bar.pulse()
158   isOutput(t, "32 columns",
159     [ [ 'up', 1 ],
160       [ 'hide' ],
161       [ 'horizontalAbsolute', 0 ],
162       [ 'write', 'NAME   / |##------------------|\n' ],
163       [ 'show' ] ])
164
165   process.stdout.columns = 16
166   bar.show("NAME", 0.5)
167   cursor = []
168   bar.last = new Date(0)
169   bar.pulse()
170   isOutput(t, "16 columns",
171     [ [ 'up', 1 ],
172       [ 'hide' ],
173       [ 'horizontalAbsolute', 0 ],
174       [ 'write', 'NAME   - |##--|\n' ],
175       [ 'show' ] ]);
176 });