File: main.coffee

package info (click to toggle)
node-external-editor 2.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 164 kB
  • sloc: makefile: 5
file content (115 lines) | stat: -rw-r--r-- 3,517 bytes parent folder | download
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
assert = require('chai').assert
readFileSync = require('fs').readFileSync
writeFileSync = require('fs').writeFileSync
IConvLite = require 'iconv-lite'
ExternalEditor = require('../../main')

testingInput = 'aAbBcCdDeEfFgG'
expectedResult = 'aAbBcCdDeE'

describe 'main', ->
  before ->
    @previous_visual = process.env.VISUAL
    process.env.VISUAL = 'truncate --size 10'

  beforeEach ->
    @editor = new ExternalEditor testingInput

  afterEach ->
    @editor.cleanup()

  after ->
    process.env.VISUAL = @previous_visual

  it 'convenience method ".edit"', ->
    text = ExternalEditor.edit testingInput
    assert.equal text, expectedResult

  it 'convenience method ".editAsync"', (cb) ->
    ExternalEditor.editAsync testingInput, (e, text) ->
      assert.equal text, expectedResult
      cb()

  it 'writes original text to file', ->
    contents = readFileSync @editor.temp_file
    assert.equal contents, testingInput

  it 'run() returns correctly', ->
    text = @editor.run()
    assert.equal text, expectedResult
    assert.equal @editor.last_exit_status, 0

  it 'runAsync() callbacks correctly', (cb) ->
    ed = @editor
    @editor.runAsync (e, text) ->
      assert.equal text, expectedResult
      assert.equal ed.last_exit_status, 0
      cb()

  it 'run() returns text same as editor.text', ->
    text = @editor.run()
    assert.equal text, @editor.text

  it 'runAsync() callback text same as editor.text', (cb) ->
    @editor.runAsync (e, text) =>
      assert.equal text, @editor.text
      cb()

describe 'invalid exit code', ->

  beforeEach ->
    @editor = new ExternalEditor testingInput
    @editor.editor.bin = "bash"
    @editor.editor.args = ["-c", "exit 1"]

  afterEach ->
    @editor.cleanup()

  it 'run()', ->
    @editor.run()
    assert.equal @editor.last_exit_status, 1

  it 'runAsync()', (cb) ->
    @editor.runAsync =>
      assert.equal @editor.last_exit_status, 1
      cb()

describe 'charsets', ->
  before ->
    @previous_visual = process.env.VISUAL
    process.env.VISUAL = 'true'

  beforeEach ->
    @editor = new ExternalEditor 'XXX'

  afterEach ->
    @editor.cleanup()

  after ->
    process.env.VISUAL = @previous_visual

  it 'empty', ->
    writeFileSync(@editor.temp_file, '')
    text = @editor.run()
    assert.equal text, ''

  it 'utf8', ->
    writeFileSync(@editor.temp_file, IConvLite.encode('काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥', 'utf8'), encoding: 'binary')
    text = @editor.run()
    assert.equal text, 'काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥'

  it 'utf16', ->
    writeFileSync(@editor.temp_file, IConvLite.encode('काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥', 'utf16'), encoding: 'binary')
    text = @editor.run()
    assert.equal text, 'काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥'

  it 'win1252', ->
    writeFileSync(@editor.temp_file, IConvLite.encode('Testing 1 2 3 ! @ #', 'win1252'), encoding: 'binary')
    text = @editor.run()
    assert.equal text, 'Testing 1 2 3 ! @ #'

  it 'Big5', ->
    writeFileSync(@editor.temp_file, IConvLite.encode('能 脊 胼 胯 臭 臬 舀 舐 航 舫 舨 般 芻 茫 荒 荔', 'Big5'), encoding: 'binary')
    text = @editor.run()
    assert.equal text, '能 脊 胼 胯 臭 臬 舀 舐 航 舫 舨 般 芻 茫 荒 荔'