import pytest
import jupytext
from jupytext.compare import compare


@pytest.mark.parametrize('lang', ['cs', 'c#', 'csharp'])
def test_simple_cs(lang):
    source = """// A Hello World! program in C#.
Console.WriteLine("Hello World!");
"""
    md = """```{lang}
{source}
```
""".format(lang=lang, source=source)
    nb = jupytext.reads(md, 'md')
    assert nb.metadata['jupytext']['main_language'] == 'csharp'
    assert len(nb.cells) == 1
    assert nb.cells[0].cell_type == 'code'

    cs = jupytext.writes(nb, 'cs')
    assert source in cs
    if lang != 'csharp':
        assert cs.startswith('// + language="{lang}"'.format(lang=lang))

    md2 = jupytext.writes(nb, 'md')
    compare(md2, md)


@pytest.mark.parametrize('lang', ['cs', 'c#', 'csharp'])
def test_csharp_magics(lang):
    md = """```{lang}
#!html
<b>Hello!</b>
```
""".format(lang=lang)
    nb = jupytext.reads(md, 'md')
    assert nb.metadata['jupytext']['main_language'] == 'csharp'
    assert len(nb.cells) == 1
    assert nb.cells[0].cell_type == 'code'

    cs = jupytext.writes(nb, 'cs')
    assert all(line.startswith('//') for line in cs.splitlines()), cs

    md2 = jupytext.writes(nb, 'md')
    md_expected = """```html
<b>Hello!</b>
```
"""
    compare(md2, md_expected)

@@ -774,16 +774,44 @@
 _magics(
+no_jupytext_version_number, 
 lang):%0A 
@@ -904,32 +904,148 @@
 reads(md, 'md')%0A
+    nb.metadata%5B'jupytext'%5D.pop('notebook_metadata_filter')%0A    nb.metadata%5B'jupytext'%5D.pop('cell_metadata_filter')%0A
     assert nb.me
@@ -1329,16 +1329,72 @@
 ed = %22%22%22
+---%0Ajupyter:%0A  jupytext:%0A    main_language: csharp%0A---%0A%0A
 %60%60%60html%0A
@@ -1445,8 +1445,392 @@
 pected)%0A
+%0A%0Adef test_read_html_cell_from_md(no_jupytext_version_number):%0A    md = %22%22%22---%0Ajupyter:%0A  jupytext:%0A    main_language: csharp%0A---%0A%0A%60%60%60html%0A%3Cb%3EHello!%3C/b%3E%0A%60%60%60%0A%22%22%22%0A%0A    nb = jupytext.reads(md, 'md')%0A    assert len(nb.cells) == 1%0A    assert nb.cells%5B0%5D.cell_type == 'code'%0A    compare(nb.cells%5B0%5D.source, %22#!html%5Cn%3Cb%3EHello!%3C/b%3E%22)%0A%0A    md2 = jupytext.writes(nb, 'md')%0A    compare(md2, md)%0A

Fix round trip on HTML/C# cells