|
| 1 | +import { LogSeverity } from './@enums/log.severity.enum'; |
| 2 | +import { SwiftLintParser } from './SwiftLintParser'; |
| 3 | + |
| 4 | +describe('SwiftLintParser tests', () => { |
| 5 | + const cwd = '/Users/master/builds/DxeaNTET/0/myswiftproject'; |
| 6 | + const mockedContent = [ |
| 7 | + { |
| 8 | + character: null, |
| 9 | + file: |
| 10 | + '/Users/master/builds/DxeaNTET/0/myswiftproject/Folder1/SubFolder1/File5.swift', |
| 11 | + line: 130, |
| 12 | + reason: 'Line should be 120 characters or less; currently it has 125 characters', |
| 13 | + rule_id: 'line_length', |
| 14 | + severity: 'Warning', |
| 15 | + type: 'Line Length', |
| 16 | + }, |
| 17 | + { |
| 18 | + character: 7, |
| 19 | + file: '', |
| 20 | + line: 9, |
| 21 | + reason: |
| 22 | + 'Type body should span 400 lines or less excluding comments and whitespace: currently spans 448 lines', |
| 23 | + rule_id: 'type_body_length', |
| 24 | + severity: 'Error', |
| 25 | + type: 'Type Body Length', |
| 26 | + }, |
| 27 | + ]; |
| 28 | + |
| 29 | + const mockedContentString = JSON.stringify(mockedContent); |
| 30 | + |
| 31 | + it('Should parse correctly', () => { |
| 32 | + const result = new SwiftLintParser(cwd).parse(mockedContentString); |
| 33 | + expect(result).toHaveLength(2); |
| 34 | + expect(result[0]).toEqual({ |
| 35 | + ruleId: 'line_length', |
| 36 | + source: `Folder1/SubFolder1/File5.swift`, |
| 37 | + severity: LogSeverity.warning, |
| 38 | + line: 130, |
| 39 | + lineOffset: 0, |
| 40 | + msg: `Line should be 120 characters or less; currently it has 125 characters`, |
| 41 | + log: JSON.stringify(mockedContent[0]), |
| 42 | + valid: true, |
| 43 | + type: 'swiftlint', |
| 44 | + }); |
| 45 | + expect(result[1]).toEqual({ |
| 46 | + ruleId: 'type_body_length', |
| 47 | + source: ``, |
| 48 | + severity: LogSeverity.error, |
| 49 | + line: 9, |
| 50 | + lineOffset: 7, |
| 51 | + msg: `Type body should span 400 lines or less excluding comments and whitespace: currently spans 448 lines`, |
| 52 | + log: JSON.stringify(mockedContent[1]), |
| 53 | + valid: false, |
| 54 | + type: 'swiftlint', |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('Should do nothing if put empty string', () => { |
| 59 | + const result = new SwiftLintParser(cwd).parse(''); |
| 60 | + expect(result).toHaveLength(0); |
| 61 | + }); |
| 62 | + |
| 63 | + it('Should parse with valid/invalid correctly', () => { |
| 64 | + const result = new SwiftLintParser(cwd).parse(mockedContentString); |
| 65 | + const valid = result.filter((el) => el.valid); |
| 66 | + const invalid = result.filter((el) => !el.valid); |
| 67 | + expect(valid).toHaveLength(1); |
| 68 | + expect(invalid).toHaveLength(1); |
| 69 | + }); |
| 70 | + |
| 71 | + it('Should throw error if the line not match the rule', () => { |
| 72 | + expect(() => new SwiftLintParser(cwd).parse(':')).toThrow(); |
| 73 | + }); |
| 74 | +}); |
0 commit comments