File: transformer_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (94 lines) | stat: -rw-r--r-- 3,392 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
# frozen_string_literal: true

require_relative '../test_helper'

describe IpynbDiff::Transformer do
  describe '.transform' do
    using RSpec::Parameterized::TableSyntax

    let!(:default_config) { { include_frontmatter: false, hide_images: false } }

    let(:test_case) { read_test_case(test_case_name) }
    let(:notebook) { test_case[:input] || FROM_IPYNB }
    let(:config) { {} }

    subject { described_class.new(**default_config.merge(config)).transform(notebook) }

    where(:ctx, :test_case_name, :config) do
      'renders metadata' | 'no_cells' | { include_frontmatter: true }
      'is empty for no cells, but metadata is false' | 'no_cells_no_metadata' | {}
      'adds markdown cell' | 'only_md' | {}
      'adds block with only one line of markdown' | 'single_line_md' | {}
      'adds raw block' | 'only_raw' | {}
      'code cell, but no output' | 'only_code' | {}
      'code cell, but no language' | 'only_code_no_language' | {}
      'code cell, but no kernelspec' | 'only_code_no_kernelspec' | {}
      'code cell, but no nb metadata' | 'only_code_no_metadata' | {}
      'text output' | 'text_output' | {}
      'ignores html output' | 'ignore_html_output' | {}
      'extracts png output along with text' | 'text_png_output' | {}
      'embeds svg as image' | 'svg' | {}
      'extracts latex output' | 'latex_output'  | {}
      'extracts error output' | 'error_output'  | {}
      'does not fetch tags if there is no cell metadata' | 'no_metadata_on_cell' | {}
      'generates :percent decorator' | 'percent_decorator' | {}
      'parses stream output' | 'stream_text' | {}
      'ignores unknown output type' | 'unknown_output_type' | {}
      'handles backslash correctly' | 'backslash_as_last_char' | {}
      'multiline png output' | 'multiline_png_output' | {}
      'hides images when option passed' | 'hide_images' | { hide_images: true }
      '\n within source lines' | 'source_with_linebreak' | { hide_images: true }
    end

    with_them do
      it 'generates the expected markdown' do
        expect(subject.as_text).to eq test_case[:expected_markdown]
      end

      it 'marks the lines correctly' do
        blocks = subject.blocks.map { |b| b[:source_symbol] }.join("\n")

        expect(blocks).to eq test_case[:expected_symbols]
      end
    end

    describe 'Source line map' do
      let(:config) { { include_frontmatter: false } }
      let(:test_case_name) { 'text_png_output' }

      it 'generates the correct transformed to source line map' do
        line_numbers = subject.blocks.map { |b| b[:source_line] }.join("\n")

        expect(line_numbers).to eq test_case[:expected_line_numbers]
      end
    end

    context 'when json is invalid' do
      let(:notebook) { 'a' }

      it 'raises error' do
        expect { subject }.to raise_error(IpynbDiff::InvalidNotebookError)
      end
    end

    context 'when it does not have the cell tag' do
      let(:notebook) { '{"metadata":[]}' }

      it 'raises error' do
        expect { subject }.to raise_error(IpynbDiff::InvalidNotebookError)
      end
    end

    context 'when notebook can not be parsed' do
      let(:notebook) { '{"cells":[]}' }

      before do
        allow(Oj::Parser.usual).to receive(:parse).and_return(nil)
      end

      it 'raises error' do
        expect { subject }.to raise_error(IpynbDiff::InvalidNotebookError)
      end
    end
  end
end