doc_examples.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: Apache-2.0
  2. use rayon::prelude::*;
  3. use solang::{
  4. codegen::{codegen, OptimizationLevel, Options},
  5. file_resolver::FileResolver,
  6. sema::diagnostics::Diagnostics,
  7. Target,
  8. };
  9. use std::{
  10. ffi::OsStr,
  11. fs::{read_dir, read_to_string},
  12. };
  13. /// Populates a file resolver with all imports that could be used by some example.
  14. fn file_resolver(target: Target) -> FileResolver {
  15. let mut result = FileResolver::new();
  16. result.set_file_contents(
  17. "docs/examples/user.sol",
  18. r##"
  19. struct User { string name; uint count; }
  20. function clear_count(User memory user) {
  21. user.count = 0;
  22. }
  23. using {clear_count} for User global;"##
  24. .into(),
  25. );
  26. if let Target::Solana = target {
  27. result.set_file_contents(
  28. "docs/examples/solana/bobcat.sol",
  29. r##"
  30. anchor_anchor constant bobcat = anchor_anchor(address'z7FbDfQDfucxJz5o8jrGLgvSbdoeSqX5VrxBb5TVjHq');
  31. interface anchor_anchor {
  32. function pounce() selector=hex"afaf6d1f0d989bed" view external returns(int64);
  33. }"##.into(),
  34. );
  35. }
  36. result
  37. }
  38. /// Returns a list of all `.sol` files in the given `dir` path.
  39. fn get_source_files(dir: &str) -> Vec<String> {
  40. read_dir(dir)
  41. .unwrap()
  42. .into_iter()
  43. .filter_map(|entry| {
  44. let e = entry.unwrap();
  45. if let (true, Some(ext)) = (e.path().is_file(), e.path().extension()) {
  46. if ext == "sol" {
  47. return Some(e.path().display().to_string());
  48. }
  49. }
  50. None
  51. })
  52. .collect()
  53. }
  54. /// Attempts to compile the file at `path` for the `target`, returning the diagnostics on fail.
  55. fn try_compile(path: &str, target: Target) -> Result<(), Diagnostics> {
  56. let mut cache = file_resolver(target);
  57. cache.set_file_contents(path, read_to_string(path).unwrap());
  58. let mut ns = solang::parse_and_resolve(OsStr::new(path), &mut cache, target);
  59. if ns.diagnostics.any_errors() {
  60. return Err(ns.diagnostics);
  61. }
  62. // Codegen should work too
  63. codegen(
  64. &mut ns,
  65. &Options {
  66. dead_storage: true,
  67. constant_folding: true,
  68. strength_reduce: true,
  69. vector_to_slice: true,
  70. math_overflow_check: true,
  71. common_subexpression_elimination: true,
  72. opt_level: OptimizationLevel::Default,
  73. ..Default::default()
  74. },
  75. );
  76. Ok(())
  77. }
  78. /// Assert no compilation errors for all `.sol` files found in `dir`.
  79. fn assert_compile(dir: &str, target: Target) {
  80. let errors = get_source_files(dir)
  81. .par_iter()
  82. .filter_map(|path| try_compile(path, target).err().map(|msg| (path, msg)))
  83. .map(|(path, msg)| println!("{} failed: {:#?}", path, msg))
  84. .count();
  85. assert_eq!(0, errors);
  86. }
  87. #[test]
  88. fn substrate_general() {
  89. assert_compile("examples", Target::default_substrate());
  90. assert_compile("docs/examples/", Target::default_substrate());
  91. }
  92. #[test]
  93. fn substrate_specific() {
  94. assert_compile("docs/examples/substrate/", Target::default_substrate());
  95. }
  96. #[test]
  97. fn solana_general() {
  98. assert_compile("examples", Target::Solana);
  99. assert_compile("docs/examples/", Target::Solana);
  100. }
  101. #[test]
  102. fn solana_specific() {
  103. assert_compile("docs/examples/solana", Target::Solana);
  104. }