Skip to content

DOCS: Move hardcoded code examples to testable files - #2472

Draft
ChoshikaBagratee wants to merge 25 commits into
developfrom
docatt-11021-move-hardcoded-code-examples
Draft

ChoshikaBagratee wants to merge 25 commits into
developfrom
docatt-11021-move-hardcoded-code-examples

Conversation

@ChoshikaBagratee

@ChoshikaBagratee ChoshikaBagratee commented Aug 12, 2026

Copy link
Copy Markdown

Description

JIRA: DOCATT-11021

Moves some of the code examples into testable files. I did not have time to move all of them.

Testing status & QA

Overall Product Risks

Please rate the potential complexity and halo effect from low to high for the reviewers. Note down potential risks to specific Editor branches if any.

  • Complexity:
  • Halo Effect:

Comments to reviewers

Checklist

Before review:

  • Changelog entry added.
    • Explains the change in Changed, Fixed, Added sections.
    • For API change contains an example snippet and/or migration example.
    • JIRA ticket linked, example (case %%). If it is a private issue, just add the case ID without a link.
    • Jira port for the next release set as "Resolved".
  • Tests added/changed, if applicable.
    • Functional tests Area_CanDoX, Area_CanDoX_EvenIfYIsTheCase, Area_WhenIDoX_AndYHappens_ThisIsTheResult.
    • Performance tests.
    • Integration tests.
  • Docs for new/changed API's.
    • Xmldoc cross references are set correctly.
    • Added explanation how the API works.
    • Usage code examples added.
    • The manual is updated, if needed.

During merge:

  • Commit message for squash-merge is prefixed with one of the list:
    • NEW: ___.
    • FIX: ___.
    • DOCS: ___.
    • CHANGE: ___.
    • RELEASE: 1.1.0-preview.3.

@cla-assistant-unity

cla-assistant-unity Bot commented Aug 12, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ josepmariapujol-unity
❌ ChoshikaBagratee
You have signed the CLA already but the status is still pending? Let us recheck it.

@ChoshikaBagratee
ChoshikaBagratee marked this pull request as ready for review August 21, 2026 19:28
@ChoshikaBagratee ChoshikaBagratee changed the title Move hardcoded code examples to testable files DOCS: Move hardcoded code examples to testable files Aug 21, 2026

@u-pr u-pr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Harness Review

Needs changes

The migration consistently redirects the affected manual pages to external source regions, but several of the newly added samples cannot be compiled together in the existing documentation-samples assembly.

Reviewed commit 8a42ed2

🤖 Helpful? 👍/👎

using UnityEngine;
using UnityEngine.InputSystem;

class BindingConflictsExample : InputTestFixture

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1

InputTestFixture is not provided by this assembly's only direct reference (Unity.InputSystem): its declaration belongs to the separate Unity.InputSystem.TestFramework assembly. Consequently this sample produces an unresolved-type compiler error in Unity.InputSystem.DocCodeSamples. Either put this test-fixture example in a test assembly that references the framework (with the appropriate test constraints) or avoid using the fixture in this compiled sample.

🤖 Helpful? 👍/👎

using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;

public class ExampleScript : MonoBehaviour

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1

These sources are all compiled into one documentation-samples assembly, so this declares DocCodeSamples.Tests.ExampleScript a second time—the same fully qualified type is already declared in ConfigureInputfromCode.cs. The same change also introduces a duplicate global MyPlayerScript between GenerateCsApiFromActions.cs and UsingDirectWorkflow.cs. C# rejects both duplicate declarations, preventing the entire samples assembly from compiling; give the snippets distinct types or namespaces.

🤖 Helpful? 👍/👎

// you change the default values for the action map, the name of the interface
// will be different.

public class MyPlayerScript : MonoBehaviour, IGameplayActions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1

The sample assembly contains neither IGameplayActions nor MyPlayerControls; the latter is only described in the comments as code the user would generate. This makes the relocated sample fail to compile (and generated callback interfaces are nested under their generated wrapper type). Add an appropriate generated test fixture/stub and implement the nested interface, or exclude this intentionally incomplete snippet from the compiled sample assembly.

🤖 Helpful? 👍/👎

// controls (For example, "<Gamepad>/<Button>").
// NOTE: The unconstrained InputStateHistory class can record changes on controls
// of different value types.
var history = new InputStateHistory<Vector2>("<Touchscreen>/primaryTouch/position");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1

InputStateHistory<TValue> is declared in UnityEngine.InputSystem.LowLevel, but this file imports only UnityEngine.InputSystem. The two examples therefore cannot resolve InputStateHistory and make the samples assembly fail to compile. Add the LowLevel using (or fully qualify the type).

🤖 Helpful? 👍/👎

@codecov-github-com

codecov-github-com Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 1.35135% with 365 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...putsystem/DocCodeSamples.Tests/MyPlayerControls.cs 0.00% 68 Missing ⚠️
...m/DocCodeSamples.Tests/IntroductionInteractions.cs 0.00% 51 Missing ⚠️
...tem/DocCodeSamples.Tests/ConfigureInputfromCode.cs 0.00% 22 Missing ⚠️
...y.inputsystem/DocCodeSamples.Tests/ControlPaths.cs 0.00% 21 Missing ⚠️
...tsystem/DocCodeSamples.Tests/ProcessorsExamples.cs 0.00% 21 Missing ⚠️
.../DocCodeSamples.Tests/RecordControlStateHistory.cs 0.00% 21 Missing ⚠️
...DocCodeSamples.Tests/HidCreateCustomLayoutClass.cs 10.00% 18 Missing ⚠️
...putsystem/DocCodeSamples.Tests/CustomProcessors.cs 15.78% 16 Missing ⚠️
...putsystem/DocCodeSamples.Tests/BindingConflicts.cs 0.00% 15 Missing ⚠️
...ystem/DocCodeSamples.Tests/ConfigureUnityEvents.cs 0.00% 15 Missing ⚠️
... and 13 more
@@             Coverage Diff             @@
##           develop    #2472      +/-   ##
===========================================
- Coverage    78.95%   78.75%   -0.20%     
===========================================
  Files          767      789      +22     
  Lines       140780   141284     +504     
===========================================
+ Hits        111153   111272     +119     
- Misses       29627    30012     +385     
Flag Coverage Δ
inputsystem_MacOS_6000.0 5.27% <0.00%> (-0.05%) ⬇️
inputsystem_MacOS_6000.0_project 77.18% <1.35%> (-0.32%) ⬇️
inputsystem_MacOS_6000.3 5.27% <0.00%> (-0.05%) ⬇️
inputsystem_MacOS_6000.3_project 77.18% <1.35%> (-0.32%) ⬇️
inputsystem_MacOS_6000.5 5.25% <0.00%> (-0.05%) ⬇️
inputsystem_MacOS_6000.5_project 77.23% <1.35%> (-0.32%) ⬇️
inputsystem_MacOS_6000.6 5.25% <0.00%> (-0.05%) ⬇️
inputsystem_MacOS_6000.6_project 77.23% <1.35%> (-0.32%) ⬇️
inputsystem_Ubuntu_6000.0 5.27% <0.00%> (-0.05%) ⬇️
inputsystem_Ubuntu_6000.0_project 77.09% <1.35%> (-0.32%) ⬇️
inputsystem_Ubuntu_6000.3 5.27% <0.00%> (-0.05%) ⬇️
inputsystem_Ubuntu_6000.3_project 77.08% <1.35%> (-0.32%) ⬇️
inputsystem_Ubuntu_6000.5 5.26% <0.00%> (-0.05%) ⬇️
inputsystem_Ubuntu_6000.5_project 77.14% <1.35%> (-0.32%) ⬇️
inputsystem_Ubuntu_6000.6 5.26% <0.00%> (-0.05%) ⬇️
inputsystem_Ubuntu_6000.6_project 77.14% <1.35%> (-0.32%) ⬇️
inputsystem_Windows_6000.0 5.27% <0.00%> (-0.05%) ⬇️
inputsystem_Windows_6000.0_project 77.31% <1.35%> (-0.26%) ⬇️
inputsystem_Windows_6000.3 5.27% <0.00%> (-0.05%) ⬇️
inputsystem_Windows_6000.3_project 77.30% <1.35%> (-0.27%) ⬇️
inputsystem_Windows_6000.5 5.25% <0.00%> (-0.05%) ⬇️
inputsystem_Windows_6000.5_project 77.36% <1.35%> (-0.27%) ⬇️
inputsystem_Windows_6000.6 5.25% <0.00%> (-0.05%) ⬇️
inputsystem_Windows_6000.6_project 77.36% <1.35%> (-0.27%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...utsystem/DocCodeSamples.Tests/ProcessorControls.cs 0.00% <0.00%> (ø)
...em/DocCodeSamples.Tests/AboutProjectWideActions.cs 0.00% <0.00%> (ø)
...inputsystem/DocCodeSamples.Tests/GamepadPolling.cs 0.00% <0.00%> (ø)
...CodeSamples.Tests/HidCreateCustomLayoutExisting.cs 0.00% <0.00%> (ø)
...putsystem/DocCodeSamples.Tests/ControlActuation.cs 0.00% <0.00%> (ø)
...m/DocCodeSamples.Tests/UsingPlayerinputWorkflow.cs 0.00% <0.00%> (ø)
...stem/DocCodeSamples.Tests/GamepadHapticsExample.cs 0.00% <0.00%> (ø)
...cCodeSamples.Tests/UsingActionsWorkflowExamples.cs 0.00% <0.00%> (ø)
...inputsystem/DocCodeSamples.Tests/DefaultActions.cs 0.00% <0.00%> (ø)
...nputsystem/DocCodeSamples.Tests/QuickStartGuide.cs 0.00% <0.00%> (ø)
... and 13 more

... and 3 files with indirect coverage changes

ℹ️ Need help interpreting these results?

Updated the changelog to include a fix for the Input Debugger window resizing issue.
@ritamerkl
ritamerkl requested review from K-Tone and suearkinunity and removed request for ritamerkl August 25, 2026 07:44
@suearkinunity

Copy link
Copy Markdown
Collaborator

@josepmariapujol-unity

Copy link
Copy Markdown
Collaborator

@u-pr review

@u-pr

u-pr Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

PR Review Summary

This pull request migrates inline code snippets from documentation markdown files into compilable C# test sample files (DocCodeSamples.Tests) referenced via DocFX [!code-cs[...](...)] tags.

Key areas to address:

  1. Incomplete snippet migration in hid-create-custom-layout-class.md: HidCreateCustomLayoutClass.cs defines regions for several code blocks (registerMyDevice, dualShock4HidInputReport, dualShock4GamepadHidBasic, dualShock4GamepadHidRegister), but only the first code block was replaced in hid-create-custom-layout-class.md.
  2. Doubled quotes in JSON snippet extraction: Embedding JSON inside C# verbatim string literals (@"...") in HidCreateCustomLayoutExisting.cs and ProcessorControls.cs causes DocFX to extract doubled quotes (e.g., ""name"" : ""MyDevice""), rendering invalid JSON in the documentation.
  3. Nested Start() method in MyExampleInteraction: In IntroductionInteractions.cs, #region custominteraction wraps a Start() method containing registration and usage code inside the IInputInteraction class.
  4. Minor typos & formatting: Incomplete path string ("*/" instead of "*/{Submit}" in ControlPaths.cs), leading spaces on a DocFX tag in about-project-wide-actions.md, and a typo in using-actions-workflow.md (fullexmaple).
📚 Additional Context
  • Packages/com.unity.inputsystem/Documentation~/hid-create-custom-layout-class.md

🤖 Helpful? 👍/👎

}

```
[!code-cs[customDeviceClass](Packages/com.unity.inputsystem/DocCodeSamples.Tests/HidCreateCustomLayoutClass.cs#customDeviceClass)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the first code snippet in this file was replaced with [!code-cs[customDeviceClass]...]. The remaining code blocks in hid-create-custom-layout-class.md (device registration, DualShock4HIDInputReport, and DualShock4GamepadHID definitions) are still inline markdown code blocks, even though corresponding regions (registerMyDevice, dualShock4HidInputReport, dualShock4GamepadHidBasic, dualShock4GamepadHidRegister) have already been set up in HidCreateCustomLayoutClass.cs.

Should the remaining code blocks in this file also be replaced with their DocFX snippet tags?

📚 Additional Context
  • Packages/com.unity.inputsystem/Documentation~/hid-create-custom-layout-class.md

🤖 Helpful? 👍/👎

internal class HidCreateCustomLayoutExisting
{
const string myDeviceJson = @"
#region myDeviceJson

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this JSON is defined inside a C# verbatim string literal (@"..."), all internal quotes are escaped as "" (e.g. ""name"" : ""MyDevice""). When DocFX extracts this region, the rendered snippet in hid-create-custom-layout-existing.md will contain doubled quotes and invalid JSON syntax.

Have you considered placing the sample JSON in a standalone .json file and referencing it directly with [!code-json[...](...)]?

🤖 Helpful? 👍/👎

}

void Start()
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start() is defined inside public class MyExampleInteraction : IInputInteraction, and falls inside #region custominteraction. As a result:

  1. The custom interaction snippet rendered in write-custom-interactions.md shows a Start() method on an IInputInteraction class (which is not a MonoBehaviour).
  2. The registration and usage samples are nested inside this class rather than shown as external setup code.

Have you considered moving Start() (or a sample method) to an external container class and closing #region custominteraction immediately after Reset()?

🤖 Helpful? 👍/👎

// Matches all gamepads (also gamepads *based* on the Gamepad layout):
_ = "<Gamepad>";
// Matches the "Submit" control on all devices:
_ = "*/";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above indicates this should match the Submit control across all devices, but the path is currently "*/".

Suggested change
_ = "*/";
// Matches the "Submit" control on all devices:
_ = "*/{Submit}";

🤖 Helpful? 👍/👎

```
InputSystem.actions.FindAction("Move");
```
[!code-cs[project-wide actions](Packages/com.unity.inputsystem/DocCodeSamples.Tests/AboutProjectWideActions.cs#about-project-wide-actions)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two leading spaces before [!code-cs...]. Removing them ensures DocFX parses the include as a top-level block element:

Suggested change
[!code-cs[project-wide actions](Packages/com.unity.inputsystem/DocCodeSamples.Tests/AboutProjectWideActions.cs#about-project-wide-actions)]
[!code-cs[project-wide actions](Packages/com.unity.inputsystem/DocCodeSamples.Tests/AboutProjectWideActions.cs#about-project-wide-actions)]

🤖 Helpful? 👍/👎

}
}
```
[!code-cs[fullexmaple](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowFullExample.cs)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo in the link text: fullexmaple -> fullexample.

Suggested change
[!code-cs[fullexmaple](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowFullExample.cs)]
[!code-cs[fullexample](Packages/com.unity.inputsystem/DocCodeSamples.Tests/UsingActionsWorkflowFullExample.cs)]

🤖 Helpful? 👍/👎

@u-pr

u-pr Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Review Summary

The migration of hardcoded documentation samples into compiled and verified C# sample files under DocCodeSamples.Tests looks great overall and successfully addresses previous compilation issues. A few adjustments remain to ensure the documentation renders accurately:

  1. JSON code samples in C# verbatim strings: In HidCreateCustomLayoutExisting.cs and ProcessorControls.cs, JSON snippets are defined inside C# verbatim string literals (@"..."). DocFX extracts lines between #region and #endregion verbatim, which will cause escaped double-quotes ("") to appear in the rendered documentation (e.g. ""name"" : ""MyDevice"").
  2. Unconverted snippets in hid-create-custom-layout-class.md: Regions were defined in HidCreateCustomLayoutClass.cs for #registerMyDevice, #dualShock4HidInputReport, #dualShock4GamepadHidBasic, and #dualShock4GamepadHidRegister, but the corresponding code blocks in hid-create-custom-layout-class.md are still hardcoded.
  3. Class snippet in IntroductionInteractions.cs: void Start() is located inside MyExampleInteraction and enclosed within #region custominteraction, causing the class definition snippet in write-custom-interactions.md to display an artificial Start() method.
  4. Minor typos in using-actions-workflow.md: Typo in "Aavoid" (line 67) and the include label [fullexmaple] (line 64).
📚 Additional Context
  • Packages/com.unity.inputsystem/Documentation~/hid-create-custom-layout-class.md
  • Packages/com.unity.inputsystem/Documentation~/add-processors-controls.md
  • Packages/com.unity.inputsystem/DocCodeSamples.Tests/DocCodeSamples.asmdef

🤖 Helpful? 👍/👎

const string myDeviceJson = @"
#region myDeviceJson
{
""name"" : ""MyDevice"",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this JSON snippet is defined inside a C# verbatim string literal (@"..."), every double quote is escaped as "". DocFX extracts the lines between #region and #endregion verbatim without string unescaping, so the rendered documentation will display doubled quotes (e.g., ""name"" : ""MyDevice"").

Have you considered placing this JSON snippet into a standalone .json file and including it via [!code-json[...](...)]?

🤖 Helpful? 👍/👎

const string json = @"
#region mydevicejson
{
""name"" : ""MyDevice"",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to HidCreateCustomLayoutExisting.cs, the JSON within this verbatim string literal uses escaped double quotes (""). When DocFX extracts #region mydevicejson, the rendered documentation snippet will contain invalid JSON with doubled quotes.

🤖 Helpful? 👍/👎

]
}
```
[!code-cs[mydevicejson](Packages/com.unity.inputsystem/DocCodeSamples.Tests/ProcessorControls.cs#mydevicejson)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet references a JSON example using [!code-cs[...]] instead of [!code-json[...]]. Consider switching the language tag to code-json once the verbatim string quoting is resolved.

📚 Additional Context
  • Packages/com.unity.inputsystem/Documentation~/add-processors-controls.md

🤖 Helpful? 👍/👎

}

```
[!code-cs[customDeviceClass](Packages/com.unity.inputsystem/DocCodeSamples.Tests/HidCreateCustomLayoutClass.cs#customDeviceClass)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While customDeviceClass has been updated to reference HidCreateCustomLayoutClass.cs, the remaining code blocks in this file are still hardcoded. You can update them to reference the corresponding regions defined in HidCreateCustomLayoutClass.cs:

  • #registerMyDevice
  • #dualShock4HidInputReport
  • #dualShock4GamepadHidBasic
  • #dualShock4GamepadHidRegister
📚 Additional Context
  • Packages/com.unity.inputsystem/Documentation~/hid-create-custom-layout-class.md

🤖 Helpful? 👍/👎

{
}

void Start()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MyExampleInteraction implements IInputInteraction rather than MonoBehaviour, and #region custominteraction wraps the entire class including void Start(). When write-custom-interactions.md includes #custominteraction, the custom interaction class snippet in the documentation will show void Start() with registration calls inside the class body.

Consider ending #region custominteraction right after Reset() and moving RegisterInteraction / InputAction setup into an example helper method outside MyExampleInteraction.

🤖 Helpful? 👍/👎

Comment thread Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md Outdated
Comment thread Packages/com.unity.inputsystem/Documentation~/using-actions-workflow.md Outdated
@josepmariapujol-unity
josepmariapujol-unity marked this pull request as draft September 15, 2026 08:15
Co-authored-by: u-pr[bot] <205906871+u-pr[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants