This repository has been archived by the owner on May 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.js
121 lines (91 loc) · 2.39 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const https = require('https')
const fs = require('fs')
const path = require('path')
const pump = require('pump')
const test = require('ava')
const ratlog = require('./index')
const testsuiteURL = 'https://raw.githubusercontent.com/ratlog/ratlog-spec/master/ratlog.testsuite.json'
const specFile = path.join(__dirname, 'spec.json')
let testcases
function loadTestcases (cb) {
fs.readFile(specFile, 'utf8', (err, contents) => {
if (err) return cb(err)
testcases = JSON.parse(contents).generic
cb()
})
}
test.serial.before.cb('ensure spec.json exists', t => {
fs.stat(specFile, err => {
if (!err) return loadTestcases(t.end)
console.log('downloading spec.json ...')
https.get(testsuiteURL, res => {
pump(res, fs.createWriteStream(specFile), () => {
console.log('download done.')
loadTestcases(t.end)
})
})
})
})
test.serial(`testing spec.json cases correctly`, t => {
t.plan(testcases.length)
testcases.forEach(({data, log}) => {
const write = line => {
t.is(line, log, 'Input:\n\n' + JSON.stringify(data, null, 2))
}
const l = ratlog(write)
l(data.message, data.fields, ...(data.tags || []))
})
})
test('initial tag', t => {
t.plan(1)
const write = line => {
t.is(line, '[tag|x|y] msg | a: 1 | b: 2\n')
}
const l = ratlog(write, 'tag')
l('msg', { a: 1, b: 2 }, 'x', 'y')
})
test('tags only', t => {
t.plan(1)
const write = line => {
t.is(line, '[x|y] msg\n')
}
const l = ratlog(write)
l('msg', 'x', 'y')
})
test('one tag only', t => {
t.plan(1)
const write = line => {
t.is(line, '[tag] msg\n')
}
const l = ratlog(write)
l('msg', 'tag')
})
test('.tag()', t => {
t.plan(1)
const write = line => {
t.is(line, '[a|b|x|y] msg | a: 1 | b: 2\n')
}
const l = ratlog(write).tag('a', 'b')
l('msg', { a: 1, b: 2 }, 'x', 'y')
})
test('.tag().tag()', t => {
t.plan(1)
const write = line => {
t.is(line, '[1|2|3|x|y] msg | a: 1 | b: 2\n')
}
const l = ratlog(write).tag(1).tag('2', 3)
l('msg', { a: 1, b: 2 }, 'x', 'y')
})
test('transform using .logger()', t => {
t.plan(1)
const write = line => {
t.is(line, '[x|y] msg | a: 1 | b: 2 | c\n')
}
const transform = log => write(ratlog.stringify({
message: 'msg',
tags: log.tags,
fields: log.fields
}))
const l = ratlog.logger(transform)
l('hey\nhey', { a: 1, b: 2, c: undefined }, ...'x', 'y')
})