@@ -720,6 +720,142 @@ x: {
720
720
}
721
721
},
722
722
},
723
+ {
724
+ name : "legend" ,
725
+
726
+ text : `
727
+ vars: {
728
+ d2-legend: {
729
+ User: "A person who interacts with the system" {
730
+ shape: person
731
+ style: {
732
+ fill: "#f5f5f5"
733
+ }
734
+ }
735
+
736
+ Database: "Stores application data" {
737
+ shape: cylinder
738
+ style.fill: "#b5d3ff"
739
+ }
740
+
741
+ HiddenShape: "This should not appear in the legend" {
742
+ style.opacity: 0
743
+ }
744
+
745
+ User -> Database: "Reads data" {
746
+ style.stroke: "blue"
747
+ }
748
+
749
+ Database -> User: "Returns results" {
750
+ style.stroke-dash: 5
751
+ }
752
+ }
753
+ }
754
+
755
+ user: User
756
+ db: Database
757
+ user -> db: Uses
758
+ ` ,
759
+ assertions : func (t * testing.T , g * d2graph.Graph ) {
760
+ if g .Legend == nil {
761
+ t .Fatal ("Expected Legend to be non-nil" )
762
+ return
763
+ }
764
+
765
+ // 2. Verify the correct objects are in the legend
766
+ if len (g .Legend .Objects ) != 2 {
767
+ t .Errorf ("Expected 2 objects in legend, got %d" , len (g .Legend .Objects ))
768
+ }
769
+
770
+ // Check for User object
771
+ hasUser := false
772
+ hasDatabase := false
773
+ for _ , obj := range g .Legend .Objects {
774
+ if obj .ID == "User" {
775
+ hasUser = true
776
+ if obj .Shape .Value != "person" {
777
+ t .Errorf ("User shape incorrect, expected 'person', got: %s" , obj .Shape .Value )
778
+ }
779
+ } else if obj .ID == "Database" {
780
+ hasDatabase = true
781
+ if obj .Shape .Value != "cylinder" {
782
+ t .Errorf ("Database shape incorrect, expected 'cylinder', got: %s" , obj .Shape .Value )
783
+ }
784
+ } else if obj .ID == "HiddenShape" {
785
+ t .Errorf ("HiddenShape should not be in legend due to opacity: 0" )
786
+ }
787
+ }
788
+
789
+ if ! hasUser {
790
+ t .Errorf ("User object missing from legend" )
791
+ }
792
+ if ! hasDatabase {
793
+ t .Errorf ("Database object missing from legend" )
794
+ }
795
+
796
+ // 3. Verify the correct edges are in the legend
797
+ if len (g .Legend .Edges ) != 2 {
798
+ t .Errorf ("Expected 2 edges in legend, got %d" , len (g .Legend .Edges ))
799
+ }
800
+
801
+ // Check for expected edges
802
+ hasReadsEdge := false
803
+ hasReturnsEdge := false
804
+ for _ , edge := range g .Legend .Edges {
805
+ if edge .Label .Value == "Reads data" {
806
+ hasReadsEdge = true
807
+ // Check edge properties
808
+ if edge .Style .Stroke == nil {
809
+ t .Errorf ("Reads edge stroke is nil" )
810
+ } else if edge .Style .Stroke .Value != "blue" {
811
+ t .Errorf ("Reads edge stroke incorrect, expected 'blue', got: %s" , edge .Style .Stroke .Value )
812
+ }
813
+ } else if edge .Label .Value == "Returns results" {
814
+ hasReturnsEdge = true
815
+ // Check edge properties
816
+ if edge .Style .StrokeDash == nil {
817
+ t .Errorf ("Returns edge stroke-dash is nil" )
818
+ } else if edge .Style .StrokeDash .Value != "5" {
819
+ t .Errorf ("Returns edge stroke-dash incorrect, expected '5', got: %s" , edge .Style .StrokeDash .Value )
820
+ }
821
+ } else if edge .Label .Value == "Hidden connection" {
822
+ t .Errorf ("Hidden connection should not be in legend due to opacity: 0" )
823
+ }
824
+ }
825
+
826
+ if ! hasReadsEdge {
827
+ t .Errorf ("'Reads data' edge missing from legend" )
828
+ }
829
+ if ! hasReturnsEdge {
830
+ t .Errorf ("'Returns results' edge missing from legend" )
831
+ }
832
+
833
+ // 4. Verify the regular diagram content is still there
834
+ userObj , hasUserObj := g .Root .HasChild ([]string {"user" })
835
+ if ! hasUserObj {
836
+ t .Errorf ("Main diagram missing 'user' object" )
837
+ } else if userObj .Label .Value != "User" {
838
+ t .Errorf ("User label incorrect, expected 'User', got: %s" , userObj .Label .Value )
839
+ }
840
+
841
+ dbObj , hasDBObj := g .Root .HasChild ([]string {"db" })
842
+ if ! hasDBObj {
843
+ t .Errorf ("Main diagram missing 'db' object" )
844
+ } else if dbObj .Label .Value != "Database" {
845
+ t .Errorf ("DB label incorrect, expected 'Database', got: %s" , dbObj .Label .Value )
846
+ }
847
+
848
+ // Check the main edge
849
+ if len (g .Edges ) == 0 {
850
+ t .Errorf ("No edges found in main diagram" )
851
+ } else {
852
+ mainEdge := g .Edges [0 ]
853
+ if mainEdge .Label .Value != "Uses" {
854
+ t .Errorf ("Main edge label incorrect, expected 'Uses', got: %s" , mainEdge .Label .Value )
855
+ }
856
+ }
857
+ },
858
+ },
723
859
{
724
860
name : "underscore_edge_nested" ,
725
861
@@ -5433,31 +5569,13 @@ b -> c
5433
5569
assert .Equal (t , "red" , g .Edges [0 ].Style .Stroke .Value )
5434
5570
},
5435
5571
},
5436
- {
5437
- name : "legend-label" ,
5438
- run : func (t * testing.T ) {
5439
- g , _ := assertCompile (t , `
5440
- a.legend-label: This is A
5441
- b: {legend-label: This is B}
5442
- a -> b: {
5443
- legend-label: "This is a->b"
5444
- }
5445
- ` , `` )
5446
- assert .Equal (t , "a" , g .Objects [0 ].ID )
5447
- assert .Equal (t , "This is A" , g .Objects [0 ].LegendLabel .Value )
5448
- assert .Equal (t , "b" , g .Objects [1 ].ID )
5449
- assert .Equal (t , "This is B" , g .Objects [1 ].LegendLabel .Value )
5450
- assert .Equal (t , "This is a->b" , g .Edges [0 ].LegendLabel .Value )
5451
- },
5452
- },
5453
5572
{
5454
5573
name : "merge-glob-values" ,
5455
5574
run : func (t * testing.T ) {
5456
5575
assertCompile (t , `
5457
5576
"a"
5458
5577
*.style.stroke-width: 2
5459
5578
*.style.font-size: 14
5460
-
5461
5579
a.width: 339
5462
5580
` , `` )
5463
5581
},
0 commit comments