import os
import sublime
from unittest import TestCase

SAMPLE_TEXT = os.path.join(os.path.dirname(__file__), 'sample.md')

class test_helloworld_command(TestCase):

    def setUp(self):
        sublime.status_message('aaaaaaaaa')
        self.view = sublime.active_window().new_file()
        # make sure we have a window to work with
        s = sublime.load_settings("Preferences.sublime-settings")
        s.set("close_windows_when_empty", False)

    # def tearDown(self):
    #     if self.view:
    #         # close file
    #         self.view.set_scratch(True)
    #         self.view.window().focus_view(self.view)
    #         self.view.window().run_command("close_file")

    def setText(self, string):
        self.view.run_command("insert", {"characters": string})

    def getRow(self, row):
        return self.view.substr(self.view.line(self.view.text_point(row, 0)))


    # ----------

    def test_hello_world(self):
        # 実行ファイルのパス取得
        testdata = open(SAMPLE_TEXT).read()
        self.setText(testdata)
        # self.view.run_command("hello_world")
        first_row = self.getRow(0)
        self.assertEqual(first_row, "# Heading 0")

@@ -3,16 +3,27 @@
 port os%0A
+import sys%0A
 import s
@@ -60,16 +60,44 @@
 stCase%0A%0A
+VERSION = sublime.version()%0A
 SAMPLE_T
@@ -156,16 +156,210 @@
 e.md')%0A%0A
+# for testing internal function%0A# if VERSION %3C '3000':%0A#     # st2%0A#     MarkdownTOC = sys.modules%5B%22MarkdownTOC%22%5D%0A# else:%0A#     # st3%0A#     MarkdownTOC = sys.modules%5B%22MarkdownTOC.MarkdownTOC%22%5D%0A%0A
 class te
@@ -417,52 +417,8 @@
 f):%0A
-        sublime.status_message('aaaaaaaaa')%0A
     
@@ -1070,16 +1070,145 @@
 , 0)))%0A%0A
+    # move cursor to%0A    def moveTo(self, pos):%0A        self.view.sel().clear()%0A        self.view.sel().add(sublime.Region(pos))%0A
 %0A    # -
@@ -1233,24 +1233,48 @@
  test_he
-llo_worl
+adings_before_TOC_will_be_ignore
 d(self):
@@ -1371,24 +1371,25 @@
 stdata)%0A
+%0A
         
 # self.v
@@ -1384,80 +1384,427 @@
     
-# self.view.run_command(%22hello_world%22)%0A        first_row = self.getRow(0
+self.moveTo(13)%0A%0A        self.view.run_command('markdowntoc_insert')%0A%0A        toc_start = self.view.find(%0A            %22%5E%3C!-- MarkdownTOC .*--%3E%5Cn%22,%0A            sublime.IGNORECASE)%0A        toc_end = self.view.find(%0A            %22%5E%3C!-- /MarkdownTOC --%3E%5Cn%22,%0A            sublime.IGNORECASE)%0A%0A        toc_txt = self.view.substr(sublime.Region(toc_start.begin(), toc_end.end()))%0A%0A        self.assertFalse('Heading 0' in toc_txt
 )%0A  
@@ -1800,32 +1800,34 @@
 toc_txt)%0A       
+ #
  self.assertEqua
@@ -1832,30 +1832,400 @@
 ual(
-first_row, %22# Heading 0%22
+toc_txt, %22%3C!-- MarkdownTOC --%3E%22)%0A        # self.assertEqual(toc_txt, %22%3C!-- MarkdownTOC --%3E%5Cn%5Cn- %5BHeading 1%5D(#heading-1)%5Cn  - %5BHeading 2%5D(#heading-2)%5Cn  - %5BHeading 3%5D(#heading-3)%5Cn- %5BHeading with anchor%5D(#with-anchor)%5Cn%5Cn%3C!-- /MarkdownTOC --%3E%22)%0A%0A%0A# class test_internal_functions(TestCase):%0A%0A#     def test_foo(self):%0A#         x = MarkdownTOC.MarkdowntocInsert(1)%0A#         self.assertEqual(x, 2
 )%0A

test passed