def test_multi_unit_operations(): xx = Unit("xx") yy = Unit("yy") assert str(xx(2) + yy(3)) == "calc(2xx + 3yy)" assert str(xx(2) + yy(3) - xx(2)) == "3yy" assert str((xx(2) + yy(3)) * 2 - yy(2)) == "calc(4xx + 4yy)" assert str((xx(100) - yy(30)) / 3) == "calc(100xx / 3 - 10yy)" assert str(xx(100) - yy(30) / 3) == "calc(100xx - 10yy)" assert str(xx(100) / 3 + yy(30) - xx(120) / 4 - yy(30)) == "calc(10xx / 3)" assert str(xx(2) + yy(3) - xx(2) - yy(9) / 3) == "0"
def test12(): # https://css-tricks.com/logic-in-media-queries/#article-header-id-8 expected = ("@media " "only screen and (min-width: 100px), " "not all and (min-width: 100px), " "not print and (min-height: 100px), " "(color), " "(min-height: 100px) and (max-height: 1000px), " "handheld and (orientation: landscape)") only, screen, min, width, all, print, height, color, max, handheld, orientation, landscape = Var.many( "only screen min width all print height color max handheld orientation landscape" ) px = Unit("px") m = media( only(screen) & {min - width: 100 * px}, ~all & {min - width: 100 * px}, ~print & {min - height: 100 * px}, {color}, _ & {min - height: 100 * px} & {max - height: 1000 * px}, handheld & {orientation: landscape}, ) assert m == expected minw = 100 * px minh = 100 * px maxh = 1000 * px m = media( only(screen) & {min - width: minw}, ~all & {min - width: minw}, ~print & {min - height: minh}, {color}, _ & {min - height: minh} & {max - height: maxh}, handheld & {orientation: landscape}, ) assert m == expected minw = {min - width: 100 * px} minh = {min - height: 100 * px} maxh = {max - height: 1000 * px} m = media( only(screen) & minw, ~all & minw, ~print & minh, {color}, _ & minh & maxh, handheld & {orientation: landscape}, ) assert m == expected
def test_unit_initialization(): xx = Unit("xx") assert str(xx(3)) == "3xx" assert str(3 * xx) == "3xx" assert str(xx * 3) == "3xx" assert str(xx(-3.1)) == "-3.1xx" assert str(-3.1 * xx) == "-3.1xx" assert str(xx * -3.1) == "-3.1xx" assert str(-xx(3.1)) == "-3.1xx" assert str(-(3.1 * xx)) == "-3.1xx" assert str(-(xx * 3.1)) == "-3.1xx"
def test_is_quantified_unit(): assert is_quantified_unit(1) is False assert is_quantified_unit(1.1) is False assert is_quantified_unit(Decimal(1.1)) is False assert is_quantified_unit(Decimal("1.1")) is False assert is_quantified_unit("1.1") is False px, em = Unit.many("px em") assert is_quantified_unit(px(3)) is True assert is_quantified_unit(px(3) * 3) is True assert is_quantified_unit(px(3) * 3 + px(2)) is True assert is_quantified_unit(px(3) * 3 + em(2)) is False
def test_is_addition_part(): assert is_addition_part(1) is False assert is_addition_part(1.1) is False assert is_addition_part(Decimal(1.1)) is False assert is_addition_part(Decimal("1.1")) is False assert is_addition_part("1.1") is False px, em = Unit.many("px em") assert is_addition_part(px(3)) is True assert is_addition_part(px(3) * 3) is True assert is_addition_part(px(3) * 3 + px(2)) is True assert is_addition_part(px(3) * 3 + em(2)) is True
def test_is_number(): assert is_number(1) is True assert is_number(1.1) is True assert is_number(Decimal(1.1)) is True assert is_number(Decimal("1.1")) is True assert is_number("1.1") is False px, em = Unit.many("px em") assert is_number(px(3)) is False assert is_number(px(3) * 3) is False assert is_number(px(3) * 3 + px(2)) is False assert is_number(px(3) * 3 + em(2)) is False
def test_var_many(): aa, bb = Var.many("aa bb") assert aa == "aa" assert isinstance(aa, Var) assert bb == "bb" assert isinstance(bb, Var) cc, dd = Var.many("cc", "dd") assert cc == "cc" assert isinstance(cc, Var) assert dd == "dd" assert isinstance(dd, Var) ee, ff = Unit.many("ee", "ff") assert ee == "ee" assert isinstance(ee, Unit) assert ff == "ff" assert isinstance(ff, Unit)
def test_unit_operations(): xx = Unit("xx") assert str(xx(3) + 2 * xx) == "5xx" assert str(xx(3) + 2) == "3xx+2" assert str(2 + xx(3)) == "2+3xx" assert str(xx(3) - 2 * xx) == "1xx" assert str(xx(3) - 3 * xx) == "0xx" assert str(xx(3) - 4 * xx) == "-1xx" assert str(xx(3) - 2) == "3xx-2" assert str(2 - xx(3)) == "2-3xx" assert str(xx(3) * 2 * xx) == "6xx*xx" assert str(xx(3) * xx(2)) == "3xx*2xx" assert str(xx(3) * 2) == "6xx" assert str(2 * xx(3)) == "6xx" assert str(xx(3) / 2 * xx) == "1.5xx*xx" assert str(xx(3) / xx(2)) == "3xx/2xx" assert str(xx(3) / 2) == "1.5xx" assert str(2 / xx(3)) == "2/3xx"
def test9(): all, max, width = Var.many("all max width") em = Unit("em") assert (media(~all & {max - width: 40 * em}) == "@media not all and (max-width: 40em)")