Kak-spec - Unit testing for Kakoune scripts!

Thanks for the encouraging comments everyone! :smiley:

@duncan It is good to hear that your dry-run worked!

With a directory structure like that, for the specs you could create a helper file spec/spec-helper.kak-no-autoload:

source "%opt(kak_spec_source_dir)/../rc/javaInterfaceHierarchy.kak"
source "%opt(kak_spec_source_dir)/../rc/javaPolicy.kak"
source "%opt(kak_spec_source_dir)/../rc/javaConstants.kak"
source "%opt(kak_spec_source_dir)/../rc/javaClassHierarchy.kak"
source "%opt(kak_spec_source_dir)/../rc/java.kak"
source "%opt(kak_spec_source_dir)/../rc/javaAnnotationEnumHierarchy.kak"

That way you can easily source your plugin in individual spec files. I looked into your repository and sketched the following spec file:

spec/regex-javax-swing.kak-spec
source "%opt(kak_spec_source_dir)/spec-helper.kak-no-autoload"

declare-option str-list saved_static_words

kak-spec \
    -title 'java filetype hook populates static_words' \
    -eval %(
        set-option window filetype java
    ) \
    '-expect-%sh(eval set -- "$kak_quoted_opt_static_words"; printf "%s\n" "$@" | sort)' \
        regex(AbsentInformationException.*ZoneRulesException)

kak-spec \
    -title 'java-constants-add adds constants to static_words' \
    -input ' javax.swing.AbstractButton' \
    -eval %(
        set-option window filetype java
        java-constants-add
    ) \
    -expect-%opt(javax_swing_abstractbutton_active) 'nay' \
    '-expect-%sh(eval set -- "$kak_quoted_opt_static_words"; printf "%s\n" "$@" | sort)' \
        regex(.*BORDER_PAINTED_CHANGED_PROPERTY.*VERTICAL_TEXT_POSITION_CHANGED_PROPERTY.*)

kak-spec \
    -title 'java-constants-add called twice adds constants to static_words only once' \
    -input ' javax.swing.AbstractButton' \
    -eval %(
        set-option window filetype java
        java-constants-add
        java-constants-add
    ) \
    -expect-%opt(javax_swing_abstractbutton_active) 'nay' \
    '-expect-%sh(eval set -- "$kak_quoted_opt_static_words"; printf "%s\n" "$@" | sort)' \
        regex(((?!BORDER_PAINTED_CHANGED_PROPERTY).)*BORDER_PAINTED_CHANGED_PROPERTY((?!BORDER_PAINTED_CHANGED_PROPERTY).)*)

kak-spec \
    -title 'java-constants-remove removes constants from static_words' \
    -input ' javax.swing.AbstractButton' \
    -eval %(
        set-option window filetype java
        java-constants-add
        java-constants-remove
    ) \
    -expect-%opt(javax_swing_abstractbutton_active) 'nop' \
    '-expect-%sh(eval set -- "$kak_quoted_opt_static_words"; printf "%s\n" "$@" | sort)' \
        regex(((?!BORDER_PAINTED_CHANGED_PROPERTY|VERTICAL_TEXT_POSITION_CHANGED_PROPERTY).)*)

Some notes about it:

  • I noticed that you use some InsertIdle hooks. Presently, those might not have time to run before kak-spec quits. Unfortunately, there is currently no way to tell kak-spec to keep running longer in order to let idle events fire. Hence, to work with kak-spec it would be best to wrap any *Idle hook callbacks in commands and to call those commands in specs.

  • In your case the static_words option is huge! Currently kak-spec does not offer any convenience features to make testing such huge arrays easier. Therefore, I had to think a bit and I considered two ways to test it:

    1. Converting the static_words list to a newline delimited string and matching that string against a regular expression. This is a compact but heuristic solution. Nevertheless, I went with this option in the above example for the compactness. The expectation arguments are a bit hard on the eyes though.
    2. Specifying a list of all the values with expectations like -expect-%opt(static_words)-( AbsentInformationException abstract AbstractMethodError ... ZoneRulesException). However, there would have been hundreds of elements to list in your case.
  • On my machine 3/4 of the tests I sketched work, but I use a kakoune version without the -remove switch to set-option that your plugin depends on. I am suspecting that the switch might be the reason for the failure and therefore I did not look into the matter further.

I hope this helps you and perhaps others to get started using kak-spec.

@jbrains That is an interesting video. Also, I agree that it would be nice for there to be more plugin development tutorials. I just found Intro to Kakoune completers by @Screwtapello which looks really nice from a quick glance. I will need to take a closer look at that.

1 Like