Skip to content

Commit

Permalink
Add tests for first class mixins (#1933)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorskees authored Oct 5, 2023
1 parent dff3578 commit c55ca91
Show file tree
Hide file tree
Showing 24 changed files with 1,791 additions and 2 deletions.
2 changes: 2 additions & 0 deletions js-api-spec/value/boolean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Sass boolean', () => {
expect(value.assertFunction).toThrow();
expect(value.assertMap).toThrow();
expect(value.tryMap()).toBe(null);
expect(value.assertMixin).toThrow();
expect(value.assertNumber).toThrow();
expect(value.assertString).toThrow();
});
Expand Down Expand Up @@ -60,6 +61,7 @@ describe('Sass boolean', () => {
expect(value.assertFunction).toThrow();
expect(value.assertMap).toThrow();
expect(value.tryMap()).toBe(null);
expect(value.assertMixin).toThrow();
expect(value.assertNumber).toThrow();
expect(value.assertString).toThrow();
});
Expand Down
1 change: 1 addition & 0 deletions js-api-spec/value/calculation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('SassCalculation', () => {
expect(() => calculation.assertFunction()).toThrow();
expect(() => calculation.assertMap()).toThrow();
expect(calculation.tryMap()).toBe(null);
expect(() => calculation.assertMixin()).toThrow();
expect(() => calculation.assertNumber()).toThrow();
expect(() => calculation.assertString()).toThrow();
});
Expand Down
1 change: 1 addition & 0 deletions js-api-spec/value/color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('SassColor', () => {
expect(() => color.assertFunction()).toThrow();
expect(() => color.assertMap()).toThrow();
expect(color.tryMap()).toBe(null);
expect(() => color.assertMixin()).toThrow();
expect(() => color.assertNumber()).toThrow();
expect(() => color.assertString()).toThrow();
});
Expand Down
2 changes: 2 additions & 0 deletions js-api-spec/value/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('SassList', () => {
expect(() => list.assertFunction()).toThrow();
expect(() => list.assertMap()).toThrow();
expect(list.tryMap()).toBe(null);
expect(() => list.assertMixin()).toThrow();
expect(() => list.assertNumber()).toThrow();
expect(() => list.assertString()).toThrow();
});
Expand Down Expand Up @@ -358,6 +359,7 @@ describe('SassList', () => {
expect(() => list.assertCalculation()).toThrow();
expect(() => list.assertColor()).toThrow();
expect(() => list.assertFunction()).toThrow();
expect(() => list.assertMixin()).toThrow();
expect(() => list.assertNumber()).toThrow();
expect(() => list.assertString()).toThrow();
});
Expand Down
1 change: 1 addition & 0 deletions js-api-spec/value/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('SassMap', () => {
expect(() => map.assertCalculation()).toThrow();
expect(() => map.assertColor()).toThrow();
expect(() => map.assertFunction()).toThrow();
expect(() => map.assertMixin()).toThrow();
expect(() => map.assertNumber()).toThrow();
expect(() => map.assertString()).toThrow();
});
Expand Down
46 changes: 46 additions & 0 deletions js-api-spec/value/mixin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import {SassMixin, compileString} from 'sass';

import {spy} from '../utils';

it('can round-trip a mixin reference from Sass', () => {
const fn = spy(args => {
expect(args).toBeArrayOfSize(1);
const value = args[0];
expect(value).toBeInstanceOf(SassMixin);
expect(value.assertCalculation).toThrow();
expect(value.assertColor).toThrow();
expect(value.assertFunction).toThrow();
expect(value.assertMap).toThrow();
expect(value.tryMap()).toBe(null);
expect(value.assertMixin()).toBe(value);
expect(value.assertNumber).toThrow();
expect(value.assertString).toThrow();

return value;
});

expect(
compileString(
`
@use 'sass:meta';
@mixin a() {
a {
b: c;
}
}
@include meta.apply(foo(meta.get-mixin('a')));
`,
{
functions: {'foo($arg)': fn},
}
).css
).toBe('a {\n b: c;\n}');

expect(fn).toHaveBeenCalled();
});
1 change: 1 addition & 0 deletions js-api-spec/value/null.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Sass null', () => {
expect(value.assertFunction).toThrow();
expect(value.assertMap).toThrow();
expect(value.tryMap()).toBe(null);
expect(value.assertMixin).toThrow();
expect(value.assertNumber).toThrow();
expect(value.assertString).toThrow();
});
Expand Down
1 change: 1 addition & 0 deletions js-api-spec/value/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('Sass number', () => {
expect(() => number.assertFunction()).toThrow();
expect(() => number.assertMap()).toThrow();
expect(number.tryMap()).toBe(null);
expect(() => number.assertMixin()).toThrow();
expect(() => number.assertString()).toThrow();
});
});
Expand Down
1 change: 1 addition & 0 deletions js-api-spec/value/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('Sass string', () => {
expect(value.assertFunction).toThrow();
expect(value.assertMap).toThrow();
expect(value.tryMap()).toBe(null);
expect(value.assertMixin).toThrow();
expect(value.assertNumber).toThrow();
});
});
Expand Down
138 changes: 138 additions & 0 deletions spec/core_functions/meta/accepts_content.hrx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<===> options.yml
---
:todo:
- sass/libsass#2807

<===>
================================================================================
<===> accepts/direct_child/input.scss
@use "sass:meta";

@mixin a() {@content}

a {b: meta.accepts-content(meta.get-mixin("a"))}

<===> accepts/direct_child/output.css
a {
b: true;
}

<===>
================================================================================
<===> accepts/nested_child/input.scss
@use "sass:meta";

@mixin a() {
@if false {@content}
}

a {b: meta.accepts-content(meta.get-mixin("a"))}

<===> accepts/nested_child/output.css
a {
b: true;
}

<===>
================================================================================
<===> accepts/builtin/input.scss
@use "sass:meta";

a {b: meta.accepts-content(meta.get-mixin(apply, meta))}

<===> accepts/builtin/output.css
a {
b: true;
}

<===>
================================================================================
<===> doesnt_accept/empty/input.scss
@use "sass:meta";
@mixin a() {}

a {b: meta.accepts-content(meta.get-mixin("a"))}

<===> doesnt_accept/empty/output.css
a {
b: false;
}

<===>
================================================================================
<===> doesnt_accept/builtin/input.scss
@use "sass:meta";
@mixin a() {}

a {b: meta.accepts-content(meta.get-mixin(load-css, meta))}

<===> doesnt_accept/builtin/output.css
a {
b: false;
}

<===>
================================================================================
<===> args/keyword/input.scss
@use "sass:meta";

a {b: meta.accepts-content($mixin: meta.get-mixin(apply, meta))}

<===> args/keyword/output.css
a {
b: true;
}

<===>
================================================================================
<===> error/args/too_few/input.scss
@use "sass:meta";

a {b: meta.accepts-content()}

<===> error/args/too_few/error
Error: Missing argument $mixin.
,--> input.scss
3 | a {b: meta.accepts-content()}
| ^^^^^^^^^^^^^^^^^^^^^^ invocation
'
,--> sass:meta
1 | @function accepts-content($mixin) {
| ======================= declaration
'
input.scss 3:7 root stylesheet

<===>
================================================================================
<===> error/args/too_many/input.scss
@use "sass:meta";
@mixin a() {}

a {b: meta.accepts-content(a, a)}

<===> error/args/too_many/error
Error: Only 1 argument allowed, but 2 were passed.
,--> input.scss
4 | a {b: meta.accepts-content(a, a)}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ invocation
'
,--> sass:meta
1 | @function accepts-content($mixin) {
| ======================= declaration
'
input.scss 4:7 root stylesheet

<===>
================================================================================
<===> error/args/wrong_type/input.scss
@use "sass:meta";

a {b: meta.accepts-content(meta.get-function("red"))}

<===> error/args/wrong_type/error
Error: $mixin: get-function("red") is not a mixin reference.
,
3 | a {b: meta.accepts-content(meta.get-function("red"))}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'
input.scss 3:7 root stylesheet
Loading

0 comments on commit c55ca91

Please sign in to comment.