|
| 1 | +describe PatternPatch::Patch do |
| 2 | + describe 'initialization' do |
| 3 | + it 'initializes parameters from options' do |
| 4 | + patch = PatternPatch::Patch.new regexp: //, text: '', mode: :prepend, global: true |
| 5 | + expect(patch.regexp).to eq(//) |
| 6 | + expect(patch.text).to eq '' |
| 7 | + expect(patch.mode).to eq :prepend |
| 8 | + expect(patch.global).to be true |
| 9 | + end |
| 10 | + |
| 11 | + it 'initializes to default values when no options passed' do |
| 12 | + patch = PatternPatch::Patch.new |
| 13 | + expect(patch.regexp).to be_nil |
| 14 | + expect(patch.text).to be_nil |
| 15 | + expect(patch.mode).to eq :append |
| 16 | + expect(patch.global).to be false |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + describe '#inspect' do |
| 21 | + it 'includes the value of each field' do |
| 22 | + text = PatternPatch::Patch.new.inspect |
| 23 | + expect(text).to match(/regexp=/) |
| 24 | + expect(text).to match(/text=/) |
| 25 | + expect(text).to match(/mode=/) |
| 26 | + expect(text).to match(/global=/) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe '#apply' do |
| 31 | + it 'passes field values to the Utilities#apply_patch method' do |
| 32 | + patch = PatternPatch::Patch.new regexp: /x/, text: 'y' |
| 33 | + expect(File).to receive(:read).with('file.txt') { 'x' } |
| 34 | + |
| 35 | + expect(PatternPatch::Utilities).to receive(:apply_patch).with( |
| 36 | + 'x', |
| 37 | + patch.regexp, |
| 38 | + patch.text, |
| 39 | + patch.global, |
| 40 | + patch.mode, 0 |
| 41 | + ) { 'xy' } |
| 42 | + |
| 43 | + expect(File).to receive(:write).with('file.txt', 'xy') |
| 44 | + |
| 45 | + patch.apply 'file.txt' |
| 46 | + end |
| 47 | + |
| 48 | + it 'passes the offset option if present' do |
| 49 | + patch = PatternPatch::Patch.new regexp: /x/, text: 'y' |
| 50 | + expect(File).to receive(:read).with('file.txt') { 'x' } |
| 51 | + |
| 52 | + expect(PatternPatch::Utilities).to receive(:apply_patch).with( |
| 53 | + 'x', |
| 54 | + patch.regexp, |
| 55 | + patch.text, |
| 56 | + patch.global, |
| 57 | + patch.mode, |
| 58 | + 1 |
| 59 | + ) { 'x' } |
| 60 | + |
| 61 | + expect(File).to receive(:write).with('file.txt', 'x') |
| 62 | + |
| 63 | + patch.apply 'file.txt', offset: 1 |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + describe '#revert' do |
| 68 | + it 'passes field values to the Utilities#revert_patch method' do |
| 69 | + patch = PatternPatch::Patch.new regexp: /x/, text: 'y' |
| 70 | + expect(File).to receive(:read).with('file.txt') { 'xy' } |
| 71 | + |
| 72 | + expect(PatternPatch::Utilities).to receive(:revert_patch).with( |
| 73 | + 'xy', |
| 74 | + patch.regexp, |
| 75 | + patch.text, |
| 76 | + patch.global, |
| 77 | + patch.mode, 0 |
| 78 | + ) { 'x' } |
| 79 | + |
| 80 | + expect(File).to receive(:write).with('file.txt', 'x') |
| 81 | + |
| 82 | + patch.revert 'file.txt' |
| 83 | + end |
| 84 | + |
| 85 | + it 'passes the offset option if present' do |
| 86 | + patch = PatternPatch::Patch.new regexp: /x/, text: 'y' |
| 87 | + expect(File).to receive(:read).with('file.txt') { 'x' } |
| 88 | + |
| 89 | + expect(PatternPatch::Utilities).to receive(:revert_patch).with( |
| 90 | + 'x', |
| 91 | + patch.regexp, |
| 92 | + patch.text, |
| 93 | + patch.global, |
| 94 | + patch.mode, |
| 95 | + 1 |
| 96 | + ) { 'x' } |
| 97 | + |
| 98 | + expect(File).to receive(:write).with('file.txt', 'x') |
| 99 | + |
| 100 | + patch.revert 'file.txt', offset: 1 |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + describe '::from_yaml' do |
| 105 | + it 'loads a patch from a YAML file' do |
| 106 | + expect(YAML).to receive(:load_file).with('file.yml') { { regexp: 'x', text: 'y', mode: 'prepend', global: true } } |
| 107 | + patch = PatternPatch::Patch.from_yaml 'file.yml' |
| 108 | + expect(patch.regexp).to eq(/x/) |
| 109 | + expect(patch.text).to eq 'y' |
| 110 | + expect(patch.mode).to eq :prepend |
| 111 | + expect(patch.global).to be true |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments