git.delta.rocks / jrsonnet / refs/commits / d5d04bbaa62b

difftreelog

source

crates/jrsonnet-formatter/src/tests.rs3.0 KiBsourcehistory
1use dprint_core::formatting::{PrintItems, PrintOptions};2use indoc::indoc;34use crate::Printable;56fn reformat(input: &str) -> String {7	let (source, _) = jrsonnet_rowan_parser::parse(input);89	dprint_core::formatting::format(10		|| {11			let mut out = PrintItems::new();12			source.print(&mut out);13			out14		},15		PrintOptions {16			indent_width: 2,17			max_width: 100,18			use_tabs: true,19			new_line_text: "\n",20		},21	)22}2324#[test]25fn complex_comments() {26	insta::assert_snapshot!(reformat(indoc!(27		"{28		  comments: {29			_: '',30			//     Plain comment31			a: '',3233			#    Plain comment with empty line before34			b: '',35			/*Single-line multiline comment3637			*/38			c: '',3940			/**Single-line multiline doc comment4142			*/43			c: '',4445			/**Multiline doc46			Comment47			*/48			c: '',4950			/*5152	Multi-line5354	comment55			*/56			d: '',5758			e: '', // Inline comment5960			k: '',6162			// Text after everything63		  },64		  comments2: {65			k: '',66			// Text after everything, but no newline above67		  },68          spacing: {69            a: '',7071            b: '',72          },73          noSpacing: {74            a: '',75            b: '',76          },7778			 smallObjectWithEnding: {/*Ending comment*/},79			 smallObjectWithFieldAndEnding: {a: 11/*Ending comment*/},80			 smallObjectWithFieldAndEnding2: {/*Start*/a: 11/*Ending comment*/},81        }"82	)));83}8485#[test]86fn args() {87	insta::assert_snapshot!(reformat(indoc!(88		"89			{90				short: aaa(1,2,3,4,5),91				long: bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),92				short_in_long: bbb(aaa(1,2,3,4,5), 123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),93				long_in_short: aaa(1,2,3,4,5,bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123)),94			}95		"96	)));97}9899#[test]100fn asserts() {101	insta::assert_snapshot!(reformat(indoc!(102		"103			{104				assert 1 > 0 : 'one should be greater than zero',105				assert true,106				value: 42,107			}108		"109	)));110}111112#[test]113fn complex_nested() {114	insta::assert_snapshot!(reformat(indoc!(115		"116			{117				kubernetes: {118				 deployment: {119					apiVersion: 'apps/v1',120					kind: 'Deployment',121					metadata: {122					  name: 'myapp',123					  labels: { app: 'myapp', version: 'v1' },124					},125					spec: {126					  replicas: 3,127					  selector: { matchLabels: { app: 'myapp' } },128					  template: {129						 metadata: { labels: { app: 'myapp' } },130						 spec: {131							containers: [132							  {133								 name: 'myapp',134								 image: 'myapp:latest',135								 ports: [{ containerPort: 8080 }],136								 env: [137									{ name: 'FOO', value: 'bar' },138									{ name: 'BAZ', valueFrom: { secretKeyRef: { name: 'mysecret', key: 'password' } } },139								 ],140							  },141							],142						 },143					  },144					},145				 },146			  },147			}148		"149	)));150}151152#[test]153fn self_super() {154	insta::assert_snapshot!(reformat(indoc!(155		"156			local base = {157			  foo: 'bar',158			  method():: self.foo,159			};160161			base {162			  foo: super.foo + '-extended',163			  result: self.method(),164			}165		"166	)));167}