livebook/test/live_book/live_markdown/export_test.exs
Jonatan Kłosko 6ac7f94897
Define notebook file format (#27)
* Initial file import/export

* Add renderer tests

* Refactor renderer

* Depend only on EarmarkParser

* Add test for export

* Add import tests

* Improve import

* Document the ExMd file format

* Rename ExMd to ExMarkdown

* Rename ExMarkdown to LiveMarkdown

* Build iodata when exporting a notebook

* Persist metadata as a single JSON object

* Move Markdown to LiveMarkdown.MarkdownHelpers

* Make LiveMarkdown private

* Always move primary heading to the top during import

* Hint the user not to use heading 1 and 2

* Return a list of messages from the import function

* Update headings warning

* Add import and export test for non-elixir snippets

* Merge markdown renderer into MarkdownHelpers

* Add import messages on AST rewrites
2021-02-16 18:39:52 +01:00

224 lines
4.9 KiB
Elixir

defmodule LiveBook.LiveMarkdown.ExportTest do
use ExUnit.Case, async: true
alias LiveBook.LiveMarkdown.Export
alias LiveBook.Notebook
test "acceptance" do
notebook = %{
Notebook.new()
| name: "My Notebook",
metadata: %{"author" => "Sherlock Holmes"},
sections: [
%{
Notebook.Section.new()
| name: "Section 1",
metadata: %{"created_at" => "2021-02-15"},
cells: [
%{
Notebook.Cell.new(:markdown)
| metadata: %{"updated_at" => "2021-02-15"},
source: """
Make sure to install:
* Erlang
* Elixir
* PostgreSQL\
"""
},
%{
Notebook.Cell.new(:elixir)
| metadata: %{"readonly" => true},
source: """
Enum.to_list(1..10)\
"""
},
%{
Notebook.Cell.new(:markdown)
| metadata: %{},
source: """
This is it for this section.\
"""
}
]
},
%{
Notebook.Section.new()
| name: "Section 2",
metadata: %{},
cells: [
%{
Notebook.Cell.new(:elixir)
| metadata: %{},
source: """
# More Elixir code\
"""
}
]
}
]
}
expected_document = """
<!--live_book:{"author":"Sherlock Holmes"}-->
# My Notebook
<!--live_book:{"created_at":"2021-02-15"}-->
## Section 1
<!--live_book:{"updated_at":"2021-02-15"}-->
Make sure to install:
* Erlang
* Elixir
* PostgreSQL
<!--live_book:{"readonly":true}-->
```elixir
Enum.to_list(1..10)
```
This is it for this section.
## Section 2
```elixir
# More Elixir code
```
"""
document = Export.notebook_to_markdown(notebook)
assert expected_document == document
end
test "reformats markdown cells" do
notebook = %{
Notebook.new()
| name: "My Notebook",
metadata: %{},
sections: [
%{
Notebook.Section.new()
| name: "Section 1",
metadata: %{},
cells: [
%{
Notebook.Cell.new(:markdown)
| metadata: %{},
source: """
|State|Abbrev|Capital|
| --: | :-: | --- |
| Texas | TX | Austin |
| Maine | ME | Augusta |
"""
}
]
}
]
}
expected_document = """
# My Notebook
## Section 1
| State | Abbrev | Capital |
| ----: | :----: | ------- |
| Texas | TX | Austin |
| Maine | ME | Augusta |
"""
document = Export.notebook_to_markdown(notebook)
assert expected_document == document
end
test "drops heading 1 and 2 in markdown cells" do
notebook = %{
Notebook.new()
| name: "My Notebook",
metadata: %{},
sections: [
%{
Notebook.Section.new()
| name: "Section 1",
metadata: %{},
cells: [
%{
Notebook.Cell.new(:markdown)
| metadata: %{},
source: """
# Heading 1
## Heading 2
### Heading 3
"""
}
]
}
]
}
expected_document = """
# My Notebook
## Section 1
### Heading 3
"""
document = Export.notebook_to_markdown(notebook)
assert expected_document == document
end
test "keeps non-elixir code snippets" do
notebook = %{
Notebook.new()
| name: "My Notebook",
metadata: %{},
sections: [
%{
Notebook.Section.new()
| name: "Section 1",
metadata: %{},
cells: [
%{
Notebook.Cell.new(:markdown)
| metadata: %{},
source: """
```shell
mix deps.get
```
```erlang
spawn_link(fun() -> io:format("Hiya") end).
```
"""
}
]
}
]
}
expected_document = """
# My Notebook
## Section 1
```shell
mix deps.get
```
```erlang
spawn_link(fun() -> io:format("Hiya") end).
```
"""
document = Export.notebook_to_markdown(notebook)
assert expected_document == document
end
end