#       Licensed to the Apache Software Foundation (ASF) under one
#       or more contributor license agreements.  See the NOTICE file
#       distributed with this work for additional information
#       regarding copyright ownership.  The ASF licenses this file
#       to you under the Apache License, Version 2.0 (the
#       "License"); you may not use this file except in compliance
#       with the License.  You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#       Unless required by applicable law or agreed to in writing,
#       software distributed under the License is distributed on an
#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#       KIND, either express or implied.  See the License for the
#       specific language governing permissions and limitations
#       under the License.

from nose.tools import assert_equal

from forgeimporters.github.utils import GitHubMarkdownConverter


class TestGitHubMarkdownConverter(object):

    def setUp(self):
        self.conv = GitHubMarkdownConverter('user', 'project')

    def test_convert_sha(self):
        text = '16c999e8c71134401a78d4d46435517b2271d6ac'
        result = self.conv.convert(text)
        assert_equal(result, '[16c999]')

        text = 'some context  16c999e8c71134401a78d4d46435517b2271d6ac '
        result = self.conv.convert(text)
        assert_equal(result, 'some context  [16c999] ')

    def test_convert_user_sha(self):
        text = 'user@16c999e8c71134401a78d4d46435517b2271d6ac'
        result = self.conv.convert(text)
        assert_equal(result, '[16c999]')

        # Not an owner of current project
        text = 'another-user@16c999e8c71134401a78d4d46435517b2271d6ac'
        result = self.conv.convert(text)
        assert_equal(result, text)

    def test_convert_user_repo_sha(self):
        text = 'user/project@16c999e8c71134401a78d4d46435517b2271d6ac'
        result = self.conv.convert(text)
        assert_equal(result, '[16c999]')

        # Not a current project
        text = 'user/p@16c999e8c71134401a78d4d46435517b2271d6ac'
        result = self.conv.convert(text)
        assert_equal(result, '[user/p@16c999]'
                             '(https://github.com/user/p/commit/16c999e8c71134401a78d4d46435517b2271d6ac)')

    def test_convert_ticket(self):
        text = 'Ticket #1'
        result = self.conv.convert(text)
        assert_equal(result, 'Ticket [#1]')
        assert_equal(self.conv.convert('#1'), '[#1]')

    def test_convert_user_ticket(self):
        text = 'user#1'
        result = self.conv.convert(text)
        assert_equal(result, '[#1]')

        # Not an owner of current project
        text = 'another-user#1'
        result = self.conv.convert(text)
        assert_equal(result, 'another-user#1')

    def test_convert_user_repo_ticket(self):
        text = 'user/project#1'
        result = self.conv.convert(text)
        assert_equal(result, '[#1]')

        # Not a current project
        text = 'user/p#1'
        result = self.conv.convert(text)
        assert_equal(result, '[user/p#1](https://github.com/user/p/issues/1)')

    def test_convert_strikethrough(self):
        text = '~~mistake~~'
        assert_equal(self.conv.convert(text), '<s>mistake</s>')

    def test_inline_code_block(self):
        text = u'This `~~some text~~` converts to this ~~strike out~~.'
        result = u'This `~~some text~~` converts to this <s>strike out</s>.'
        assert_equal(self.conv.convert(text).strip(), result)

    def test_convert_code_blocks(self):
        text = u'''```python
print "Hello!"
```

Two code blocks here!

```
for (var i = 0; i < a.length; i++) {
    console.log(i);
}
```'''
        result = u''':::python
    print "Hello!"

Two code blocks here!

    for (var i = 0; i < a.length; i++) {
        console.log(i);
    }'''

        assert_equal(self.conv.convert(text).strip(), result)

    def test_code_blocks_without_newline_before(self):
        text = u'''
There are some code snippet:
```
print 'Hello'
```
Pretty cool, ha?'''

        result = u'''
There are some code snippet:

    print 'Hello'
Pretty cool, ha?'''
        assert_equal(self.conv.convert(text).strip(), result.strip())
        text = text.replace('```', '~~~')
        assert_equal(self.conv.convert(text).strip(), result.strip())

        text = u'''
There are some code snippet:
```python
print 'Hello'
```
Pretty cool, ha?'''

        result = u'''
There are some code snippet:

    :::python
    print 'Hello'
Pretty cool, ha?'''
        assert_equal(self.conv.convert(text).strip(), result.strip())
