MDL-61394 core_scss: Add unit tests for SassC compilation

This patch also adds a test in theme_boost to verify that boost
can be succesfully compiled without errors by SassC.
This commit is contained in:
Cameron Ball
2018-02-19 14:46:00 +08:00
parent 8a5613f643
commit eadebb2445
3 changed files with 125 additions and 2 deletions
+1 -1
View File
@@ -1475,7 +1475,7 @@ class theme_config {
// Compile!
$compiled = $compiler->to_css();
} catch (\Leafo\ScssPhp\Exception $e) {
} catch (\Exception $e) {
$compiled = false;
debugging('Error while compiling SCSS: ' . $e->getMessage(), DEBUG_DEVELOPER);
}
+74 -1
View File
@@ -70,6 +70,61 @@ class core_scss_testcase extends advanced_testcase {
];
}
/**
* Test cases for SassC compilation.
*/
public function scss_compilation_provider() {
return [
'simple' => [
'scss' => '$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}',
'expected' => <<<CSS
body {
font: 100% Helvetica, sans-serif;
color: #333; }
CSS
],
'nested' => [
'scss' => 'nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}',
'expected' => <<<CSS
nav ul {
margin: 0;
padding: 0;
list-style: none; }
nav li {
display: inline-block; }
nav a {
display: block;
padding: 6px 12px;
text-decoration: none; }
CSS
]
];
}
/**
* @dataProvider is_valid_file_provider
*/
@@ -78,4 +133,22 @@ class core_scss_testcase extends advanced_testcase {
$pathvalid = phpunit_util::call_internal_method($scss, 'is_valid_file', [$path], \core_scss::class);
$this->assertSame($valid, $pathvalid);
}
}
/**
* Test that we can use the SassC compiler if it's provided.
*
* @dataProvider scss_compilation_provider
* @param string $scss The raw scss to compile.
* @param string $expectedcss The expected CSS output.
*/
public function test_scss_compilation_with_sassc($scss, $expectedcss) {
if (!defined('PHPUNIT_PATH_TO_SASSC')) {
$this->markTestSkipped('Path to SassC not provided');
}
$this->resetAfterTest();
set_config('pathtosassc', PHPUNIT_PATH_TO_SASSC);
$compiler = new core_scss();
$this->assertSame($compiler->compile($scss), $expectedcss);
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the unittests for boost's scss compilation.
*
* @package theme_boost
* @copyright 2018 Cameron Ball <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for scss compilation.
*
* @package theme_boost
* @copyright 2016 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class theme_boost_scss_testcase extends advanced_testcase {
/**
* Test that boost can be compiled using SassC (the defacto implemention).
*/
public function test_scss_compilation_with_sassc() {
if (!defined('PHPUNIT_PATH_TO_SASSC')) {
$this->markTestSkipped('Path to SassC not provided');
}
$this->resetAfterTest();
set_config('pathtosassc', PHPUNIT_PATH_TO_SASSC);
$this->assertNotEmpty(
theme_config::load('boost')->get_css_content_debug('scss', null, null)
);
}
}