New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse Srcset Attribute #18313
Closed
+291
−4
Closed
Parse Srcset Attribute #18313
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
Loading status checks…
Test cases for parse srcset attribute
- Loading branch information
commit 4c427f4565e11ce560cd2502e248ed904a199028
| @@ -0,0 +1,84 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| use script::test::srcset::{Descriptor, ImageSource, parse_a_srcset_attribute}; | ||
|
|
||
| #[test] | ||
| fn no_value() { | ||
| let new_vec = Vec::new(); | ||
| assert_eq!(parse_a_srcset_attribute(" "), new_vec); | ||
| } | ||
|
|
||
| #[test] | ||
| fn width_one_value() { | ||
| let first_descriptor = Descriptor { wid: Some(320), den: None }; | ||
| let first_imagesource = ImageSource { url: "small-image.jpg".to_string(), descriptor: first_descriptor }; | ||
| let sources = &[first_imagesource]; | ||
| assert_eq!(parse_a_srcset_attribute("small-image.jpg, 320w"), sources); | ||
| } | ||
|
|
||
| #[test] | ||
| fn width_two_value() { | ||
| let first_descriptor = Descriptor { wid: Some(320), den: None }; | ||
| let first_imagesource = ImageSource { url: "small-image.jpg".to_string(), descriptor: first_descriptor }; | ||
| let second_descriptor = Descriptor { wid: Some(480), den: None }; | ||
| let second_imagesource = ImageSource { url: "medium-image.jpg".to_string(), descriptor: second_descriptor }; | ||
| let sources = &[first_imagesource, second_imagesource]; | ||
| assert_eq!(parse_a_srcset_attribute("small-image.jpg 320w, medium-image.jpg 480w"), sources); | ||
| } | ||
|
|
||
| #[test] | ||
| fn width_three_value() { | ||
| let first_descriptor = Descriptor { wid: Some(320), den: None }; | ||
| let first_imagesource = ImageSource { url: "smallImage.jpg".to_string(), descriptor: first_descriptor }; | ||
| let second_descriptor = Descriptor { wid: Some(480), den: None }; | ||
| let second_imagesource = ImageSource { url: "mediumImage.jpg".to_string(), descriptor: second_descriptor }; | ||
| let third_descriptor = Descriptor { wid: Some(800), den: None }; | ||
| let third_imagesource = ImageSource { url: "largeImage.jpg".to_string(), descriptor: third_descriptor }; | ||
| let sources = &[first_imagesource, second_imagesource, third_imagesource]; | ||
| assert_eq!(parse_a_srcset_attribute("smallImage.jpg 320w, | ||
| mediumImage.jpg 480w, | ||
| largeImage.jpg 800w"), sources); | ||
| } | ||
|
|
||
| #[test] | ||
| fn density_value() { | ||
| let first_descriptor = Descriptor { wid: None, den: Some(1.0) }; | ||
| let first_imagesource = ImageSource { url: "small-image.jpg".to_string(), descriptor: first_descriptor }; | ||
| let sources = &[first_imagesource]; | ||
| assert_eq!(parse_a_srcset_attribute("small-image.jpg 1x"), sources); | ||
| } | ||
|
|
||
| #[test] | ||
| fn without_descriptor() { | ||
| let first_descriptor = Descriptor { wid: None, den: None }; | ||
| let first_imagesource = ImageSource { url: "small-image.jpg".to_string(), descriptor: first_descriptor }; | ||
| let sources = &[first_imagesource]; | ||
| assert_eq!(parse_a_srcset_attribute("small-image.jpg"), sources); | ||
| } | ||
|
|
||
| //Does not parse an ImageSource when both width and density descriptor present | ||
| #[test] | ||
| fn two_descriptor() { | ||
| let empty_vec = Vec::new(); | ||
| assert_eq!(parse_a_srcset_attribute("small-image.jpg 320w 1.1x"), empty_vec); | ||
| } | ||
|
|
||
| #[test] | ||
|
||
| fn decimal_descriptor() { | ||
| let first_descriptor = Descriptor { wid: None, den: Some(2.2) }; | ||
| let first_imagesource = ImageSource { url: "small-image.jpg".to_string(), descriptor: first_descriptor }; | ||
| let sources = &[first_imagesource]; | ||
| assert_eq!(parse_a_srcset_attribute("small-image.jpg 2.2x"), sources); | ||
| } | ||
|
|
||
| #[test] | ||
| fn different_descriptor() { | ||
| let first_descriptor = Descriptor { wid: Some(320), den: None }; | ||
| let first_imagesource = ImageSource { url: "small-image.jpg".to_string(), descriptor: first_descriptor }; | ||
| let second_descriptor = Descriptor { wid: None, den: Some(2.2) }; | ||
| let second_imagesource = ImageSource { url: "medium-image.jpg".to_string(), descriptor: second_descriptor }; | ||
| let sources = &[first_imagesource, second_imagesource]; | ||
| assert_eq!(parse_a_srcset_attribute("small-image.jpg 320w, medium-image.jpg 2.2x"), sources); | ||
| } | ||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Nit: newline between functions please