import os

import pytest
import sphinx
from sphinx import addnodes
from sphinx.builders.html import SingleFileHTMLBuilder, DirectoryHTMLBuilder

from .util import build_all


def test_basic():
    for (app, status, warning) in build_all('test-basic'):
        assert app.env.get_doctree('index').traverse(addnodes.toctree)
        content = open(os.path.join(app.outdir, 'index.html')).read()

        if isinstance(app.builder, DirectoryHTMLBuilder):
            search = (
                '<div class="toctree-wrapper compound">\n'
                '<ul>\n'
                '<li class="toctree-l1">'
                '<a class="reference internal" href="foo/">foo</a>'
                '<ul>\n'
                '<li class="toctree-l2">'
                '<a class="reference internal" href="bar/">bar</a></li>\n'
                '</ul>\n'
                '</li>\n'
                '</ul>\n'
                '</div>'
            )
            assert search in content
        elif isinstance(app.builder, SingleFileHTMLBuilder):
            search = (
                '<ul>\n'
                '<li class="toctree-l1">'
                '<a class="reference internal" href="index.html#document-foo">foo</a>'
                '</li>\n'
                '</ul>'
            )
            assert search in content
        else:
            search = (
                '<div class="toctree-wrapper compound">\n'
                '<ul>\n'
                '<li class="toctree-l1">'
                '<a class="reference internal" href="foo.html">foo</a>'
                '<ul>\n'
                '<li class="toctree-l2">'
                '<a class="reference internal" href="bar.html">bar</a></li>\n'
                '</ul>\n'
                '</li>\n'
                '</ul>\n'
                '</div>'
            )
            assert search in content, ('Missing search with builder {0}'
                                       .format(app.builder.name))


def test_empty():
    """Local TOC is showing, as toctree was empty"""
    for (app, status, warning) in build_all('test-empty'):
        assert app.env.get_doctree('index').traverse(addnodes.toctree)
        content = open(os.path.join(app.outdir, 'index.html')).read()
        if sphinx.version_info < (1, 4):
            if isinstance(app.builder, SingleFileHTMLBuilder):
                assert '<div class="toctree-wrapper compound">\n</div>' in content
                assert '<div class="local-toc">' in content
            else:
                global_toc = (
                    '<div class="toctree-wrapper compound">\n'
                    '<ul class="simple">\n</ul>\n'
                    '</div>'
                )
                local_toc = (
                    '<div class="local-toc"><ul class="simple">'
                    '</ul>\n</div>'
                )
                assert global_toc in content
                assert local_toc not in content
        else:
            global_toc = '<div class="toctree-wrapper compound">\n</div>'
            local_toc = (
                '<div class="local-toc"><ul>\n'
                '<li><a class="reference internal" href="#">test-empty</a></li>'
                '</ul>\n</div>'
            )
            assert global_toc in content
            assert local_toc not in content


def test_missing_toctree():
    """Local TOC is showing, as toctree was missing"""
    for (app, status, warning) in build_all('test-missing-toctree'):
        assert app.env.get_doctree('index').traverse(addnodes.toctree) == []
        content = open(os.path.join(app.outdir, 'index.html')).read()
        assert '<div class="toctree' not in content
        assert '<div class="local-toc">' in content

@@ -2219,465 +2219,70 @@
     
-if sphinx.version_info %3C (1, 4):%0A            if isinstance(app.builder, SingleFileHTMLBuilder):%0A                assert '%3Cdiv class=%22toctree-wrapper compound%22%3E%5Cn%3C/div%3E' in content%0A                assert '%3Cdiv class=%22local-toc%22%3E' in content%0A            else:%0A                global_toc = (%0A                    '%3Cdiv class=%22toctree-wrapper compound%22%3E%5Cn'%0A                    '%3Cul class=%22simple%22%3E%5Cn%3C/ul%3E%5Cn'%0A                    '%3C/div%3E'%0A                )%0A        
+global_toc = '%3Cdiv class=%22toctree-wrapper compound%22%3E%5Cn%3C/div%3E'%0A
     
@@ -2307,32 +2307,24 @@
             
-  
-      
 '%3Cdiv class=
@@ -2342,339 +2342,13 @@
 %3E%3Cul
- class=%22simple%22%3E'%0A                    '%3C/ul%3E%5Cn%3C/div%3E'%0A                )%0A                assert global_toc in content%0A                assert local_toc not in content%0A        else:%0A            global_toc = '%3Cdiv class=%22toctree-wrapper compound%22%3E%5Cn%3C/div%3E'%0A            local_toc = (%0A                '%3Cdiv class=%22local-toc%22%3E%3Cul%3E%5Cn'%0A    
+%3E%5Cn'%0A
     
@@ -2424,36 +2424,32 @@
 i%3E'%0A            
-    
 '%3C/ul%3E%5Cn%3C/div%3E'%0A
@@ -2452,34 +2452,26 @@
 v%3E'%0A        
- 
-   )%0A    
+)%0A
         asse
@@ -2491,28 +2491,24 @@
  in content%0A
-    
         asse

Remove test for sphinx version no longer supported (#1071)