"""Tests for the Compass images functions.

Not all of Compass is implemented, and the arrangement of Compass functions
doesn't exactly match the arrangement in the original documentation.
Regardless, this is a good starting place:

http://compass-style.org/reference/compass/helpers/

Some functions appear to be undocumented, but nonetheless are part of Compass's
Ruby code.
"""

from scss.expression import Calculator
from scss.functions.compass.images import COMPASS_IMAGES_LIBRARY
from scss.rule import Namespace


import pytest
from scss import config
import os
from _pytest.monkeypatch import monkeypatch
xfail = pytest.mark.xfail

# TODO many of these tests could also stand to test for failure cases


@pytest.fixture
def calc():
    ns = Namespace(functions=COMPASS_IMAGES_LIBRARY)
    return Calculator(ns).evaluate_expression


def test_image_url(calc):
    assert calc('image-url("/some_path.jpg")').render() == ('url(%(images_url)ssome_path.jpg)' % {'images_url': config.IMAGES_URL})
    

# inline-image
def test_inline_image(calc):
    monkeypatch().setattr(config, 'IMAGES_ROOT', os.path.join(config.PROJECT_ROOT, 'tests/files/images'))
    
    f = open(os.path.join(config.PROJECT_ROOT, 'tests/files/images/test-qr.base64.txt'), 'rb')
    font_base64 = f.read()
    f.close()
    assert 'url(data:image/png;base64,%s)' % font_base64 == calc('inline_image("/test-qr.png")').render()
    

def test_inline_cursor(calc):
    monkeypatch().setattr(config, 'IMAGES_ROOT', os.path.join(config.PROJECT_ROOT, 'tests/files/cursors'))
    
    f = open(os.path.join(config.PROJECT_ROOT, 'tests/files/cursors/fake.base64.txt'), 'rb')
    font_base64 = f.read()
    f.close()
    assert 'url(data:image/cur;base64,%s)' % font_base64 == calc('inline_image("/fake.cur")').render()



# for debugging uncomment next lines
if __name__=='__main__':
#    test_image_url(calc())
#    test_inline_image(calc())
#    test_inline_cursor(calc())

@@ -991,21 +991,17 @@
 S_URL%7D)%0A
-    
 %0A
+
 %0A# inlin
@@ -1139,29 +1139,25 @@
 s/images'))%0A
-    
 %0A
+
     f = open
@@ -1386,21 +1386,17 @@
 ender()%0A
-    
 %0A
+
 %0Adef tes
@@ -1529,13 +1529,9 @@
 '))%0A
-    
 %0A
+
     
@@ -1652,32 +1652,32 @@
 )%0A    f.close()%0A
+
     assert 'url(
@@ -1767,160 +1767,4 @@
 r()%0A
-%0A%0A%0A# for debugging uncomment next lines%0Aif __name__=='__main__':%0A#    test_image_url(calc())%0A#    test_inline_image(calc())%0A#    test_inline_cursor(calc())%0A

Delete errant `if` block from test.